Skip to content

Commit e90e6ec

Browse files
committed
[Graph] programmers μˆœμœ„
1 parent d575d2f commit e90e6ec

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

β€ŽREADME.md

+1
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@
286286
| 08 | | [Baekjoon-21276 계보 볡원가 ν˜Έμ„](./src/Graph/P21276) | [μœ„μƒμ •λ ¬](./notes/Graph/TopologicalSort.md) |
287287
| 09 | ⭐️ | [Baekjoon-2458 ν‚€ μˆœμ„œ](./src/Graph/P2458) | |
288288
| 10 | | [Programmers κ°€μž₯ λ¨Ό λ…Έλ“œ](./src/Graph/prg49189) | |
289+
| 11 | | [Programmers μˆœμœ„](./src/Graph/prg49191) | |
289290

290291
### Minimum Spanning Tree (MST)
291292

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

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## [programmers - κ·Έλž˜ν”„] μˆœμœ„
2+
3+
![image](https://user-images.githubusercontent.com/22045163/116101038-77f37700-a6e8-11eb-91c3-02a1bb84052c.png)
4+
5+
### μœ μ‚¬ν•œ 문제
6+
7+
[BOJ 2458 ν‚€ μˆœμ„œ](../P2458) λ¬Έμ œμ™€ λΉ„μŠ·ν•˜λ©° 풀이도 λ™μΌν•˜κ²Œ ν•΄μ„œ ν’€μ—ˆλ‹€.

β€Žsrc/Graph/prg49191/Solution.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package Graph.prg49191;
2+
3+
class Solution {
4+
5+
int[][] graph;
6+
7+
public int solution(int n, int[][] results) {
8+
9+
graph = new int[n+1][n+1];
10+
for (int[] res : results) {
11+
graph[res[0]][res[1]] = 1;
12+
} // graph[A][B] == 1 : AλŠ” Bλ₯Ό 이겼닀.
13+
14+
for (int i = 1; i <= n; i++) {
15+
graph[i][0] = -1;
16+
}
17+
18+
for (int i = 1; i <= n; i++) {
19+
dfs(i, n);
20+
}
21+
22+
for (int i = 1; i <= n; i++) {
23+
for (int j = 1; j <= n; j++) {
24+
graph[0][j] += graph[i][j];
25+
}
26+
}
27+
28+
int ans = 0;
29+
for (int i = 1; i <= n; i++) {
30+
if (graph[0][i] + graph[i][0] == n-1) ans ++;
31+
}
32+
33+
return ans;
34+
}
35+
36+
void dfs(int cur, int n) {
37+
38+
for (int i = 1; i <= n; i++) {
39+
if (graph[cur][i] == 1) {
40+
if (graph[i][0] == -1) dfs(i, n);
41+
for (int j = 1; j <= n; j++) {
42+
if (graph[i][j] == 1) graph[cur][j] = 1;
43+
}
44+
}
45+
}
46+
47+
int cnt = 0;
48+
for (int i = 1; i <= n; i++) {
49+
cnt += graph[cur][i];
50+
}
51+
graph[cur][0] = cnt;
52+
}
53+
}

0 commit comments

Comments
Β (0)