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 168
168
169
169
### π [ Graph NOTE] ( ./notes/Graph/Graph.md )
170
170
171
+ ### Graph
172
+
173
+ | # | β | Problem | Note |
174
+ | :-: | :-: | :------------------------------------------------------- | :--- |
175
+ | 01 | | [ Baekjoon-11724 μ°κ²° μμμ κ°μ] ( ./src/Graph/P11724 ) | |
176
+
171
177
### Union-Find
172
178
173
179
| # | β | Problem | Note |
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ ## [ baekjoon-11724] μ°κ²° μμμ κ°μ
2
+
3
+ ![ image] ( https://user-images.githubusercontent.com/22045163/96466954-632fb580-1265-11eb-8b38-670c7276869b.png )
Original file line number Diff line number Diff line change
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
You canβt perform that action at this time.
0 commit comments