Skip to content

Commit b810971

Browse files
committed
이예진: [BOJ] 9466 텀 프로젝트_241121
1 parent fc8bda9 commit b810971

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

BOJ/5001-10000번/YJ_9466.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
//2 ≤ n ≤ 10^5
5+
public class YJ_9466 {
6+
static int[] graph;
7+
static boolean[] visited;
8+
static boolean[] isFinished;
9+
static boolean[] isGrouping;
10+
11+
public static void main(String[] args) throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st = new StringTokenizer(br.readLine());
14+
int T = Integer.parseInt(st.nextToken());
15+
16+
//테스트케이스 실행
17+
while(T-- > 0){
18+
//초기화
19+
int n = Integer.parseInt(br.readLine());
20+
graph = new int[n+1];
21+
isFinished = new boolean[n+1];
22+
visited = new boolean[n+1];
23+
isGrouping = new boolean[n+1];
24+
st = new StringTokenizer(br.readLine());
25+
for(int i=1; i<n+1; i++){
26+
graph[i] = Integer.parseInt(st.nextToken());
27+
}
28+
//dfs 탐색
29+
for(int i=1; i<n+1; i++){
30+
if(!visited[i]){
31+
dfs(i);
32+
}
33+
}
34+
//프로젝트 팀에 속하지 못한 학생 수
35+
int count = 0;
36+
for(int i=1; i<n+1; i++){
37+
if(!isGrouping[i]){
38+
count++;
39+
}
40+
}
41+
System.out.println(count);
42+
}
43+
44+
}
45+
46+
public static void dfs(int current){
47+
visited[current] = true;
48+
int next= graph[current];
49+
50+
if(!visited[next]){
51+
dfs(next);
52+
} else if(!isFinished[next]) { //★재방문이지만 이전에 사이클을 이미 만들었거나, 사이클 형성을 못한 경우
53+
//싸이클 형성 가능 또는 자기자신
54+
while(!isGrouping[next]){
55+
isGrouping[next] = true;
56+
next = graph[next];
57+
}
58+
}
59+
isFinished[current] = true;
60+
}
61+
62+
}

0 commit comments

Comments
 (0)