-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp11725.java
More file actions
56 lines (46 loc) · 1.37 KB
/
p11725.java
File metadata and controls
56 lines (46 loc) · 1.37 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
import java.io.*;
import java.util.*;
public class p11725 {
static int N;
static BufferedReader br;
static StringTokenizer st;
static List<Integer>[] list;
static boolean[] visit;
static int[] parents;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
list = new ArrayList[N + 1];
visit = new boolean[N + 1];
parents = new int[N + 1];
for (int i = 1; i <= N; i++) {
list[i] = new ArrayList<>();
}
for (int i = 1; i < N; i++) {
st = new StringTokenizer(br.readLine());
int u = Integer.parseInt(st.nextToken());
int v = Integer.parseInt(st.nextToken());
list[u].add(v);
list[v].add(u);
}
bfs(1);
for (int i = 2; i <= N; i++) {
System.out.println(parents[i]);
}
}
static void bfs(int start) {
Queue<Integer> q = new LinkedList<>();
q.add(start);
visit[start] = true;
while(!q.isEmpty()) {
int cur = q.poll();
for (int x : list[cur]) {
if(!visit[x]) {
q.add(x);
visit[x] = true;
parents[x] = cur;
}
}
}
}
}