-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboj_10451.java
More file actions
43 lines (38 loc) · 1.12 KB
/
boj_10451.java
File metadata and controls
43 lines (38 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package dfs_bfs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class boj_10451 {
static int t;
static int[] arr;
static boolean[] chk;
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
t=Integer.parseInt(br.readLine());
for(int i=0;i<t;i++){
int res=0;
int n=Integer.parseInt(br.readLine());
arr=new int[n+1];
chk=new boolean[n+1];
StringTokenizer st=new StringTokenizer(br.readLine());
for(int j=1;j<=n;j++){
arr[j]=Integer.parseInt(st.nextToken());
}
for(int k=1;k<=n;k++){
if(!chk[k]){
dfs(k);
res++;
}
}
System.out.println(res);
}
}
public static void dfs(int start){ //재귀로 구현
chk[start]=true;
int p=arr[start];
if(!chk[p]){
dfs(arr[start]);
}
}
}