728x90
반응형
문제
제출한 답안
package BOJ;
import java.util.ArrayList;
import java.util.Scanner;
public class BOJ_11725 {
private static ArrayList<ArrayList<Integer>> tree = new ArrayList<>();
private static boolean[] isVisited;
private static int[] parents;
private static int N;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = Integer.parseInt(sc.nextLine());
isVisited = new boolean[N+1];
parents = new int[N+1];
for (int i = 0; i <= N; i++) {
tree.add(new ArrayList<>());
}
for (int i = 0; i < N - 1; i++) {
String[] split = sc.nextLine().split(" ");
int x = Integer.parseInt(split[0]);
int y = Integer.parseInt(split[1]);
tree.get(x).add(y);
tree.get(y).add(x);
}
sc.close();
dfs(1);
for (int i = 2; i < parents.length; i++) {
System.out.println(parents[i]);
}
}
private static void dfs(int vertex){
isVisited[vertex] = true;
ArrayList<Integer> list = tree.get(vertex);
for (int i : list) {
if (!isVisited[i]){
parents[i] = vertex;
dfs(i);
}
}
}
}
728x90
반응형
'Algorithm' 카테고리의 다른 글
[BOJ 2644] 촌수계산 ( with Java ) (0) | 2023.04.17 |
---|---|
[BOJ 2583] 영역 구하기 ( with Java ) (0) | 2023.04.17 |
[BOJ 1987] 알파벳 ( with Java ) (0) | 2023.04.17 |
[BOJ 2468] 안전 영역 ( with Java ) (0) | 2023.04.16 |
[BOJ 4963] 섬의 개수 ( with Java ) (0) | 2023.04.16 |