-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBAEKJOON_11725
More file actions
57 lines (51 loc) · 1.53 KB
/
BAEKJOON_11725
File metadata and controls
57 lines (51 loc) · 1.53 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
57
import java.io.*;
import java.sql.Array;
import java.util.*;
public class Main {
static int N;
static ArrayList<Integer>[] list;
static boolean[] visit;
static int[] arr;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
N = Integer.parseInt(br.readLine());
list = new ArrayList[N + 1];
visit = new boolean[N + 1];
arr = new int[N + 1];
for(int i=1; i<N+1; i++){
list[i] = new ArrayList<>();
}
StringTokenizer st;
for(int i=1; i<N; i++){
st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
list[x].add(y);
list[y].add(x);
}
visit[1]=true;
BFS(1);
for(int i=2; i<N+1; i++){
bw.write(String.valueOf(arr[i]));
bw.newLine();
}
bw.flush();
bw.close();
}
public static void BFS(int start){
Queue<Integer> q = new LinkedList<>();
q.add(start);
while(!q.isEmpty()){
int x = q.poll();
for (int i : list[x]) {
int next = i;
if(!visit[next]){
visit[next]=true;
arr[next]=x;
q.add(next);
}
}
}
}
}