-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
50 lines (39 loc) · 1.05 KB
/
Main.java
File metadata and controls
50 lines (39 loc) · 1.05 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static int cnt;
static boolean[] visited;
static ArrayList<Integer>[] conn;
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
conn = new ArrayList[N+1];
for(int i=0; i<conn.length; i++){
conn[i] = new ArrayList<Integer>();
}
for(int i=1; i<M+1; i++){
int c1 = sc.nextInt();
int c2 = sc.nextInt();
conn[c1].add(c2);
conn[c2].add(c1);
}
visited = new boolean[N+1];
Arrays.fill(visited, Boolean.FALSE);
dfs(1);
System.out.println(cnt-1);
sc.close();
}
public static void dfs(int computer){
cnt++;
visited[computer] = true;
for(int c : conn[computer]){
if(!visited[c]){
dfs(c);
}
}
}
}