File tree Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Expand file tree Collapse file tree 3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 ) λ¬Έμ μ λΉμ·νλ©° νμ΄λ λμΌνκ² ν΄μ νμλ€.
Original file line number Diff line number Diff line change 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+ }
You canβt perform that action at this time.
0 commit comments