Skip to content

Commit 9b8ff5d

Browse files
authored
Merge pull request #1070 from AlgorithmWithGod/zinnnn37
[20251008] BOJ / G3 / 텀 프로젝트 / 김민진
2 parents 3d6233f + 61e9361 commit 9b8ff5d

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
```java
2+
import java.io.*;
3+
import java.util.StringTokenizer;
4+
5+
public class BJ_9466_텀_프로젝트 {
6+
7+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
9+
private static final StringBuilder sb = new StringBuilder();
10+
private static StringTokenizer st;
11+
12+
private static int N, cnt;
13+
private static int[] group;
14+
private static boolean[] visited, finished;
15+
16+
public static void main(String[] args) throws IOException {
17+
int T = Integer.parseInt(br.readLine());
18+
19+
while (T-- > 0) {
20+
init();
21+
sol();
22+
}
23+
bw.write(sb.toString());
24+
bw.flush();
25+
bw.close();
26+
br.close();
27+
}
28+
29+
private static void init() throws IOException {
30+
N = Integer.parseInt(br.readLine());
31+
32+
cnt = 0;
33+
group = new int[N + 1];
34+
visited = new boolean[N + 1];
35+
finished = new boolean[N + 1];
36+
37+
st = new StringTokenizer(br.readLine());
38+
for (int i = 1; i <= N; i++) {
39+
group[i] = Integer.parseInt(st.nextToken());
40+
}
41+
}
42+
43+
private static void sol() throws IOException {
44+
for (int i = 1; i <= N; i++) {
45+
dfs(i);
46+
}
47+
sb.append(N - cnt).append("\n");
48+
}
49+
50+
private static void dfs(int node) {
51+
visited[node] = true;
52+
53+
int next = group[node];
54+
55+
if (!visited[next]) {
56+
dfs(next);
57+
} else if (!finished[next]) {
58+
while (next != node) {
59+
cnt++;
60+
next = group[next];
61+
}
62+
cnt++;
63+
}
64+
finished[node] = true;
65+
}
66+
67+
}
68+
```

0 commit comments

Comments
 (0)