-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_11724.java
51 lines (44 loc) · 1.2 KB
/
number_11724.java
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.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class number_11724 {
static LinkedList<Integer> ajlist[];
static Boolean visit[];
static int count=0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
ajlist = new LinkedList[n+1];
visit = new Boolean[n+1];
Arrays.fill(visit, false);
for(int i=0;i<n+1;i++){
ajlist[i] = new LinkedList<>();
}
for(int i=0;i<m;i++){
int from = sc.nextInt();
int to = sc.nextInt();
ajlist[from].add(to);
ajlist[to].add(from);
}sc.close();
for(int i=1;i<n+1;i++){
if(DFS(i)){
count+=1;
}
}
System.out.println(count);
}
static Boolean DFS(int v){
if(visit[v]){
return false;
}
visit[v] = true;
for(int i=0;i<ajlist[v].size();i++){
int current_node = ajlist[v].get(i);
if(!visit[current_node]){
DFS(current_node);
}
}
return true;
}
}