File tree 4 files changed +57
-0
lines changed
4 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 165
165
| 03 | | [ SWEA-1233 사칙연산 유효성 검사] ( ./src/Tree/swea1233 ) | |
166
166
| 04 | ⭐️ | [ Baekjoon-1167 트리의 지름] ( ./src/Tree/P1167 ) | |
167
167
| 05 | ⭐️ | [ Baekjoon-2263 트리의 순회] ( ./src/Tree/P2263 ) | |
168
+ | 06 | | [ Baekjoon-11725 트리의 부모 찾기] ( ./src/Tree/P11725 ) | |
168
169
169
170
### Heap
170
171
Original file line number Diff line number Diff line change
1
+ package Tree .P11725 ;
2
+
3
+ import java .io .*;
4
+ import java .util .*;
5
+
6
+ public class Main {
7
+
8
+ static int N ;
9
+ static int [] parents ;
10
+ static ArrayList <Integer >[] edges ;
11
+
12
+ public static void main (String [] args ) throws Exception {
13
+ System .setIn (new FileInputStream ("src/Tree/P11725/input.txt" ));
14
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
15
+
16
+ N = Integer .parseInt (br .readLine ());
17
+
18
+ parents = new int [N +1 ];
19
+ edges = new ArrayList [N +1 ];
20
+ for (int i = 1 ; i <= N ; i ++) edges [i ] = new ArrayList <>();
21
+
22
+ for (int i = 0 ; i < N -1 ; i ++) {
23
+ StringTokenizer st = new StringTokenizer (br .readLine ());
24
+ int a = Integer .parseInt (st .nextToken ()), b = Integer .parseInt (st .nextToken ());
25
+ edges [a ].add (b );
26
+ edges [b ].add (a );
27
+ }
28
+
29
+ Queue <Integer > q = new LinkedList <>();
30
+ q .offer (1 );
31
+ while (!q .isEmpty ()) {
32
+ int cur = q .poll ();
33
+ for (int e : edges [cur ]) {
34
+ if (parents [e ] > 0 ) continue ;
35
+ parents [e ] = cur ;
36
+ q .offer (e );
37
+ }
38
+ }
39
+
40
+ StringBuilder sb = new StringBuilder ();
41
+ for (int i = 2 ; i <= N ; i ++) sb .append (parents [i ]).append ("\n " );
42
+ System .out .println (sb .toString ());
43
+ }
44
+ }
Original file line number Diff line number Diff line change
1
+ ## [ baekjoon-11725] 트리의 부모 찾기
2
+
3
+ ![ image] ( https://user-images.githubusercontent.com/22045163/108373876-b6ca0400-7243-11eb-9cc2-f36377d877aa.png )
4
+
5
+ ![ image] ( https://user-images.githubusercontent.com/22045163/108373917-bfbad580-7243-11eb-898b-72bfee6cab19.png )
Original file line number Diff line number Diff line change
1
+ 7
2
+ 1 6
3
+ 6 3
4
+ 3 5
5
+ 4 1
6
+ 2 4
7
+ 4 7
You can’t perform that action at this time.
0 commit comments