-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ11725.java
More file actions
48 lines (41 loc) · 1.31 KB
/
BOJ11725.java
File metadata and controls
48 lines (41 loc) · 1.31 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class BOJ11725 {
static int N;
static boolean[] visited;
static ArrayList<Integer> graph[];
static int[] parent;
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
visited = new boolean[N+1];
graph = new ArrayList[N+1];
parent = new int[N+1];
for(int i = 1; i <= N; i++){
graph[i] = new ArrayList<Integer>();
}
for(int i = 1; i <= N-1; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
graph[x].add(y);
graph[y].add(x);
}
dfs(1);
for(int i = 2; i <= N; i++){
System.out.println(parent[i]);
}
}
public static void dfs(int n){
visited[n] = true;
for(int i : graph[n]){
if(!visited[i]){
parent[i] = n;
dfs(i);
}
}
}
}