Skip to content

Commit a98bb76

Browse files
committed
11724 연결 요소의 개수
1 parent f73b587 commit a98bb76

File tree

1 file changed

+59
-36
lines changed

1 file changed

+59
-36
lines changed
Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,78 @@
11
package dfs.silver.silver_2;
2-
import java.io.BufferedReader;
3-
import java.io.IOException;
4-
import java.io.InputStreamReader;
5-
import java.util.ArrayList;
2+
3+
import java.io.*;
4+
//import java.util.Stack;
65
import java.util.StringTokenizer;
6+
77
public class _11724 {
8-
static int N, M, cnt = 0;
9-
static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
10-
static boolean[] visited;
118

12-
public static void dfs(int startNode) {
13-
if(visited[startNode]) {
14-
return;
15-
}
16-
else {
17-
visited[startNode] = true;
9+
private static int n;
10+
private static int m;
1811

19-
for(int node : graph.get(startNode)) {
20-
if(!visited[node]) {
21-
dfs(node);
22-
}
23-
}
24-
}
25-
}
26-
public static void main(String[] args) throws IOException{
12+
private static int[][] graph;
13+
private static boolean[] visited;
14+
15+
public static void main(String[] args) throws IOException {
2716
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
2818
StringTokenizer st = new StringTokenizer(br.readLine());
2919

30-
N = Integer.parseInt(st.nextToken());
31-
M = Integer.parseInt(st.nextToken());
20+
n = Integer.parseInt(st.nextToken());
21+
m = Integer.parseInt(st.nextToken());
3222

33-
visited = new boolean[N + 1];
23+
graph = new int[n + 1][n + 1];
24+
visited = new boolean[n + 1];
3425

35-
for(int i = 0; i <= N; i++) {
36-
graph.add(new ArrayList<>());
37-
}
38-
for(int i = 0; i < M; i++) {
26+
for (int i = 0; i < m; i++) {
3927
st = new StringTokenizer(br.readLine());
4028

41-
int a = Integer.parseInt(st.nextToken());
42-
int b = Integer.parseInt(st.nextToken());
29+
int x = Integer.parseInt(st.nextToken());
30+
int y = Integer.parseInt(st.nextToken());
4331

44-
graph.get(a).add(b);
45-
graph.get(b).add(a);
32+
graph[x][y] = 1;
33+
graph[y][x] = 1;
4634
}
47-
for(int i = 1; i <= N; i++) {
48-
if(!visited[i]) {
35+
36+
int count = 0;
37+
38+
for (int i = 1; i <= n; i++) {
39+
if (!visited[i]) {
4940
dfs(i);
50-
cnt++;
41+
count++;
5142
}
5243
}
53-
System.out.println(cnt);
44+
45+
bw.write(String.valueOf(count));
46+
bw.flush();
47+
bw.close();
5448
}
49+
50+
private static void dfs(int node) {
51+
visited[node] = true;
52+
53+
for (int i = 1; i <= n; i++) {
54+
if (!visited[i] && graph[node][i] == 1) {
55+
visited[i] = true;
56+
dfs(i);
57+
}
58+
}
59+
}
60+
61+
// private static void dfs(int startNode) {
62+
// Stack<Integer> stack = new Stack<>();
63+
// stack.push(startNode);
64+
// visited[startNode] = true;
65+
//
66+
// while (!stack.isEmpty()) {
67+
// int cur = stack.pop();
68+
//
69+
// for (int i = 1; i <= n; i++) {
70+
// if (!visited[i] && graph[cur][i] == 1) {
71+
// visited[i] = true;
72+
// stack.push(i);
73+
// }
74+
// }
75+
// }
76+
// }
77+
5578
}

0 commit comments

Comments
 (0)