Skip to content

Commit 84fa02f

Browse files
committed
[Graph] baekjoon-11724
1 parent 4e7646b commit 84fa02f

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

β€ŽREADME.md

+6
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@
168168

169169
### πŸ“– [Graph NOTE](./notes/Graph/Graph.md)
170170

171+
### Graph
172+
173+
| # | β˜† | Problem | Note |
174+
| :-: | :-: | :------------------------------------------------------- | :--- |
175+
| 01 | | [Baekjoon-11724 μ—°κ²° μš”μ†Œμ˜ 개수](./src/Graph/P11724) | |
176+
171177
### Union-Find
172178

173179
| # | β˜† | Problem | Note |

β€Žsrc/Graph/P11724/Main.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package Graph.P11724;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
static int N, M;
9+
static LinkedList<Integer>[] graph;
10+
static boolean[] visited;
11+
static int ans = 0;
12+
13+
public static void main(String[] args) throws Exception {
14+
System.setIn(new FileInputStream("src/Graph/P11724/input.txt"));
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
18+
N = Integer.parseInt(st.nextToken());
19+
M = Integer.parseInt(st.nextToken());
20+
21+
graph = new LinkedList[N+1];
22+
visited = new boolean[N+1];
23+
for (int i = 1; i <= N; i++) graph[i] = new LinkedList<>();
24+
25+
for (int i = 0; i < M; i++) {
26+
st = new StringTokenizer(br.readLine());
27+
int u = Integer.parseInt(st.nextToken());
28+
int v = Integer.parseInt(st.nextToken());
29+
graph[u].add(v);
30+
graph[v].add(u);
31+
}
32+
33+
for (int i = 1; i <= N; i++) {
34+
if (!visited[i]) {
35+
dfs(i);
36+
ans ++;
37+
}
38+
}
39+
40+
System.out.println(ans);
41+
}
42+
43+
static void dfs(int node) {
44+
visited[node] = true;
45+
for (int i = 0; i < graph[node].size(); i++) {
46+
int target = graph[node].get(i);
47+
if (!visited[target]) {
48+
dfs(target);
49+
}
50+
}
51+
}
52+
}

β€Žsrc/Graph/P11724/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## [baekjoon-11724] μ—°κ²° μš”μ†Œμ˜ 개수
2+
3+
![image](https://user-images.githubusercontent.com/22045163/96466954-632fb580-1265-11eb-8b38-670c7276869b.png)

β€Žsrc/Graph/P11724/input.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
6 8
2+
1 2
3+
2 5
4+
5 1
5+
3 4
6+
4 6
7+
5 4
8+
2 4
9+
2 3

0 commit comments

Comments
Β (0)