import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static ArrayList<Integer>[] graph;
static int N;
static boolean[] visited;
static int[] parents;
static void DFS(int index) {
for(int x: graph[index]) {
if(!visited[x]) {
visited[x] = true;
parents[x] = index;
DFS(x);
}
}
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
N = kb.nextInt();
graph = new ArrayList[N+1];
visited = new boolean[N+1];
parents = new int[N+1];
for(int i=0; i<N+1; i++) {
graph[i] = new ArrayList<>();
}
for(int i=0; i<N-1; i++) {
int a = kb.nextInt();
int b = kb.nextInt();
graph[a].add(b);
graph[b].add(a);
}
DFS(1);
StringBuilder sb = new StringBuilder();
for(int i=2; i<=N; i++) {
sb.append(parents[i]+"\n");
}
System.out.println(sb.toString());
}
}
'공부기록 > 백준' 카테고리의 다른 글
[백준] 11399번 ATM (0) | 2024.02.04 |
---|---|
[백준] 9996번 한국이 그리울 땐 서버에 접속하지 (0) | 2024.02.04 |
[백준] 2407번 조합 (1) | 2024.02.03 |
[백준] 2563번 색종이 (0) | 2024.01.31 |
[백준] 11724번 연결요소의 개수 구하기 (0) | 2024.01.17 |