Skip to content

Commit b49006d

Browse files
committed
[Graph] baekjoon-2458
1 parent dc3f607 commit b49006d

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

Diff for: โ€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@
276276
| 06 | | [SWEA-3289 ์„œ๋กœ์†Œ ์ง‘ํ•ฉ](./src/Graph/swea3289) | union-find |
277277
| 07 | | [JUNGOL-1863 ์ข…๊ต](./src/Graph/jo1863) | union-find : union-by-rank |
278278
| 08 | | [Baekjoon-21276 ๊ณ„๋ณด ๋ณต์›๊ฐ€ ํ˜ธ์„](./src/Graph/P21276) | [์œ„์ƒ์ •๋ ฌ](./notes/Graph/TopologicalSort.md) |
279+
| 09 | โญ๏ธ | [Baekjoon-2458 ํ‚ค ์ˆœ์„œ](./src/Graph/P2458) | |
279280

280281
### Minimum Spanning Tree (MST)
281282

Diff for: โ€Žsrc/Graph/P2458/Main.java

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package Graph.P2458;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
static int N, M, next_cnt;
9+
static LinkedList<Integer>[] graph;
10+
static int[] cnt;
11+
static boolean[] visited;
12+
13+
public static void main(String[] args) throws Exception {
14+
System.setIn(new FileInputStream("src/Graph/P5643/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+
cnt = new int[N+1];
23+
for (int i = 1; i <= N; i++) {
24+
graph[i] = new LinkedList<>();
25+
}
26+
27+
while (M-- > 0) {
28+
st = new StringTokenizer(br.readLine());
29+
graph[Integer.parseInt(st.nextToken())].add(Integer.parseInt(st.nextToken()));
30+
}
31+
32+
for (int i = 1; i <= N; i++) {
33+
visited= new boolean[N+1];
34+
next_cnt = 0;
35+
dfs(i);
36+
cnt[i] += next_cnt;
37+
}
38+
39+
int ans = 0;
40+
for (int i = 1; i <= N; i++) {
41+
if (cnt[i] == N) ans ++;
42+
}
43+
44+
System.out.println(ans);
45+
}
46+
47+
static void dfs(int cur) {
48+
visited[cur] = true;
49+
next_cnt ++;
50+
for (int next : graph[cur]) {
51+
if (!visited[next]) {
52+
cnt[next] ++;
53+
dfs(next);
54+
}
55+
}
56+
}
57+
}

Diff for: โ€Žsrc/Graph/P2458/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## [baekjoon-2458] ํ‚ค ์ˆœ์„œ
2+
3+
![image](https://user-images.githubusercontent.com/22045163/115832330-bcafb180-a44d-11eb-9cdc-3b736178cbd4.png)
4+
5+
![image](https://user-images.githubusercontent.com/22045163/115832369-ca653700-a44d-11eb-8322-a91daa866817.png)

Diff for: โ€Žsrc/Graph/P2458/input.txt

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

0 commit comments

Comments
ย (0)