-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ11724_2.java
More file actions
55 lines (43 loc) · 1.39 KB
/
Copy pathJ11724_2.java
File metadata and controls
55 lines (43 loc) · 1.39 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
44
45
46
47
48
49
50
51
52
53
54
55
package TIL;
import java.util.*;
public class J11724_2 {
static ArrayList<Integer>[] arrayList;
static boolean[] visited;// 초기값은 false로 초기화된다.
static int count = 0;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int e = scanner.nextInt();
arrayList = new ArrayList[n + 1];
visited = new boolean[n+1];
// for(int i = 0 ; i < n+1; i++)
// System.out.println("visited"+"["+i+"] = "+ visited[i]);
for (int i = 1; i <= n; i++) {
arrayList[i] = new ArrayList<Integer>();
}
for (int i = 0; i < e; i++) {
int value_1 = scanner.nextInt();
int value_2 = scanner.nextInt();
arrayList[value_1].add(value_2);
arrayList[value_2].add(value_1);
}
// 여기부터 다시 알아보기
for(int i= 1; i<=n ; i++)
if(!visited[i]) {
count++;
DFS(i);
}
System.out.println(count);
}
public static void DFS(int n) {
if(visited[n])
return;
visited[n] = true;
// 여기가 이해가 안되는 거임
for(int i = 0; i < arrayList[n].size(); i++){
int node = arrayList[n].get(i);
if (!visited[node])
DFS(node);
}
}
}