-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy path[15]_1.java
43 lines (34 loc) · 817 Bytes
/
[15]_1.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
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static int n, m, result;
public static ArrayList<Integer>[] adj = new ArrayList[101];
public static boolean[] visited = new boolean[101];
// 깊이 우선 탐색(DFS)
public static void dfs(int x) {
result++;
visited[x] = true;
for (int i = 0; i < adj[x].size(); i++) {
int y = adj[x].get(i);
if (!visited[y]) {
dfs(y);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
m = sc.nextInt();
for (int i = 1; i <= n; i++) {
adj[i] = new ArrayList<Integer>();
}
for (int i = 0; i < m; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
adj[x].add(y);
adj[y].add(x);
}
dfs(1);
System.out.println(result - 1);
}
}