File tree 4 files changed +70
-0
lines changed
4 files changed +70
-0
lines changed Original file line number Diff line number Diff line change 276
276
| 06 | | [ SWEA-3289 ์๋ก์ ์งํฉ] ( ./src/Graph/swea3289 ) | union-find |
277
277
| 07 | | [ JUNGOL-1863 ์ข
๊ต] ( ./src/Graph/jo1863 ) | union-find : union-by-rank |
278
278
| 08 | | [ Baekjoon-21276 ๊ณ๋ณด ๋ณต์๊ฐ ํธ์] ( ./src/Graph/P21276 ) | [ ์์์ ๋ ฌ] ( ./notes/Graph/TopologicalSort.md ) |
279
+ | 09 | โญ๏ธ | [ Baekjoon-2458 ํค ์์] ( ./src/Graph/P2458 ) | |
279
280
280
281
### Minimum Spanning Tree (MST)
281
282
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
1
+ 6 6
2
+ 1 5
3
+ 3 4
4
+ 5 4
5
+ 4 2
6
+ 4 6
7
+ 5 2
You canโt perform that action at this time.
0 commit comments