-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboj_11725.java
More file actions
49 lines (42 loc) · 1.23 KB
/
boj_11725.java
File metadata and controls
49 lines (42 loc) · 1.23 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
package dfs_bfs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class boj_11725 {
static int n;
static int[] parent;
static ArrayList<Integer> arr[];
static boolean[] visit;
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
parent=new int[n+1];
arr=new ArrayList[n+1];
for(int i=0;i<n+1;i++){
arr[i]=new ArrayList<>();
}
visit=new boolean[n+1];
for(int i=0;i<n-1;i++){
StringTokenizer st=new StringTokenizer(br.readLine());
int a=Integer.parseInt(st.nextToken());
int b=Integer.parseInt(st.nextToken());
arr[a].add(b);
arr[b].add(a);
}
dfs(1);
for(int i=2;i<=n;i++){
System.out.println(parent[i]);
}
}
public static void dfs(int level){
visit[level]=true;
for(int i: arr[level]){
if(!visit[i]){
parent[i]=level;
dfs(i);
}
}
}
}