- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ11724_6.java
More file actions
Latest commit
53 lines (45 loc) · 1.37 KB
/
Copy pathJ11724_6.java
File metadata and controls
53 lines (45 loc) · 1.37 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
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.ArrayList;
importjava.util.LinkedList;
publicclassJ11724_6 {
staticintn, m;
staticLinkedList<Integer>[] list;
staticbooleanvisited[];
publicstaticvoidmain(String[] args) throwsIOException {
BufferedReaderbuffer = newBufferedReader(newInputStreamReader(System.in));
String[] input = buffer.readLine().split(" ");
n = Integer.parseInt(input[0]);
m = Integer.parseInt(input[1]);
list = newLinkedList[n+1];
for(inti = 0 ; i <= n; i++){
list[i] = newLinkedList<>();
}
visited = newboolean[n+1];
for(inti = 0 ; i < m; i++){
input = buffer.readLine().split(" ");
intstart = Integer.parseInt(input[0]);
intend = Integer.parseInt(input[1]);
list[start].add(end);
list[end].add(start);
}
intcnt = 0;
for(inti =1 ; i<= n; i++){
if(!visited[i]){
dfs(i);
cnt++;
}
}
System.out.println(cnt);
}
publicstaticvoiddfs(inta){
// if(visited[a]) return;
visited[a] = true;
for(intnext : list[a]){
if(!visited[next]){
dfs(next);
}
}
}
}