-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ11724_6.java
More file actions
53 lines (45 loc) · 1.37 KB
/
J11724_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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
public class J11724_6 {
static int n, m;
static LinkedList<Integer>[] list;
static boolean visited[];
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String[] input = buffer.readLine().split(" ");
n = Integer.parseInt(input[0]);
m = Integer.parseInt(input[1]);
list = new LinkedList[n+1];
for(int i = 0 ; i <= n; i++){
list[i] = new LinkedList<>();
}
visited = new boolean[n+1];
for(int i = 0 ; i < m; i++){
input = buffer.readLine().split(" ");
int start = Integer.parseInt(input[0]);
int end = Integer.parseInt(input[1]);
list[start].add(end);
list[end].add(start);
}
int cnt = 0;
for(int i =1 ; i<= n; i++){
if(!visited[i]){
dfs(i);
cnt++;
}
}
System.out.println(cnt);
}
public static void dfs(int a){
// if(visited[a]) return;
visited[a] = true;
for(int next : list[a]){
if(!visited[next]){
dfs(next);
}
}
}
}