-
Notifications
You must be signed in to change notification settings - Fork 0
/
B_11724
51 lines (43 loc) · 1.28 KB
/
B_11724
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
import java.util.*;
import java.io.*;
public class main {
static ArrayList<Integer>[] arr;
static boolean[] c;
static boolean[] ac;
static int N,M;
static int ans = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
N = Integer.parseInt(s.split(" ")[0]);
M = Integer.parseInt(s.split(" ")[1]);
arr = new ArrayList[N + 1];
c = new boolean[N + 1];
ac = new boolean[N + 1];
for (int i = 1; i < N + 1; i++) {
arr[i] = new ArrayList();
}
for (int i = 0; i < M; i++) {
String s1 = br.readLine();
int u = Integer.parseInt(s1.split(" ")[0]);
int v = Integer.parseInt(s1.split(" ")[1]);
arr[u].add(v);
arr[v].add(u);
}
for (int i = 1; i < N + 1; i++) {
if(ac[i]) continue;
c = new boolean[N + 1];
dfs(i);
ans++;
}
System.out.println(ans);
}
public static void dfs(int start) {
c[start] = true;
ac[start] = true;
for (int v : arr[start]) {
if (c[v]) continue;
dfs(v);
}
}
}