-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ11724.java
More file actions
53 lines (46 loc) · 1.25 KB
/
Copy pathJ11724.java
File metadata and controls
53 lines (46 loc) · 1.25 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
package TIL;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class J11724 {
static boolean visited[];
static ArrayList<Integer>[] A;
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String[] input = bufferedReader.readLine().split(" ");
int n = Integer.parseInt(input[0]);
int m = Integer.parseInt(input[1]);
visited = new boolean[n+1];
A = new ArrayList[n+1];
for(int i = 1; i <= n; i++){
A[i] = new ArrayList<Integer>();
}
for(int i=0; i<m; i++){
input = bufferedReader.readLine().split(" ");
int s = Integer.parseInt(input[0]);
int e = Integer.parseInt(input[1]);
A[s].add(e);
A[e].add(s);
}
int count = 0;
for(int i = 1; i <=n; i++){
if(!visited[i]) {
count++;
DFS(i);
}
}
System.out.println(count);
}
public static void DFS(int v){
if(visited[v]){
return;
}
visited[v] = true;
for(int i : A[v]){
if(!visited[i]) {
DFS(i);
}
}
}
}