-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2606.java
More file actions
52 lines (32 loc) · 975 Bytes
/
2606.java
File metadata and controls
52 lines (32 loc) · 975 Bytes
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
import java.io.*;
import java.util.*;
public class Main {
static boolean[] check;
static int[][] arr;
static int count = 0;
static int node, line;
static Queue<Integer> q = new LinkedList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
node = Integer.parseInt(br.readLine());
line = Integer.parseInt(br.readLine());
arr = new int[node+1][node+1];
check = new boolean[node+1];
for(int i = 0 ; i < line ; i ++) {
StringTokenizer str = new StringTokenizer(br.readLine());
int a = Integer.parseInt(str.nextToken());
int b = Integer.parseInt(str.nextToken());
arr[a][b] = arr[b][a] = 1;
}
dfs(1);
System.out.println(count-1);
}
public static void dfs(int start) {
check[start] = true;
count++;
for(int i = 0 ; i <= node ; i++) {
if(arr[start][i] == 1 && !check[i])
dfs(i);
}
}
}