-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.java
44 lines (35 loc) Β· 1.28 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package Tree.P11725;
import java.io.*;
import java.util.*;
public class Main {
static int N;
static int[] parents;
static ArrayList<Integer>[] edges;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Tree/P11725/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
parents = new int[N+1];
edges = new ArrayList[N+1];
for (int i = 1; i <= N; i++) edges[i] = new ArrayList<>();
for (int i = 0; i < N-1; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken()), b = Integer.parseInt(st.nextToken());
edges[a].add(b);
edges[b].add(a);
}
Queue<Integer> q = new LinkedList<>();
q.offer(1);
while (!q.isEmpty()) {
int cur = q.poll();
for (int e : edges[cur]) {
if (parents[e] > 0) continue;
parents[e] = cur;
q.offer(e);
}
}
StringBuilder sb = new StringBuilder();
for (int i = 2; i <= N; i++) sb.append(parents[i]).append("\n");
System.out.println(sb.toString());
}
}