-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ2606.java
More file actions
48 lines (39 loc) · 1.34 KB
/
BJ2606.java
File metadata and controls
48 lines (39 loc) · 1.34 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
import java.io.*;
import java.util.StringTokenizer;
public class BJ2606{
static boolean[][] graph;
static boolean[] visited;
static int N, M;
static int answer;
public static void dfs(int idx) {
visited[idx] = true;
answer++;
for (int i = 1; i < N; i++) {
if(visited[i] == false && graph[idx][i] == true)
dfs(i);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
N = Integer.parseInt(br.readLine());
M = Integer.parseInt(br.readLine());
graph = new boolean[N + 1][N + 1];
visited = new boolean[N + 1];
// 1. 자료구조에 값 채우기
for (int i = 0; i < M; i++) {
int x, y;
StringTokenizer st = new StringTokenizer(br.readLine());
x = Integer.parseInt(st.nextToken());
y = Integer.parseInt(st.nextToken());
graph[x][y] = true; // 나머지 위치의 요소들은 자동으로 false인가?
graph[y][x] = true;
}
// 2. DFS 호출
dfs(1);
// 3. 출력
bw.write(String.valueOf(answer - 1));
br.close();
bw.close();
}
}