https://www.acmicpc.net/problem/11725
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main_bj_11725_트리의부모찾기 {
static int N;
static int[] parent;
static boolean[] visit;
static List[] nodes;
public static void main(String[] args) throws Exception{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
N=Integer.parseInt(br.readLine());
parent=new int[N+1];
visit=new boolean[N+1];
nodes=new ArrayList[N+1];
for(int i=1; i<=N; i++) {
nodes[i]=new ArrayList();
}
for(int i=1; i<=N-1; i++) {
st=new StringTokenizer(br.readLine()," ");
int num1=Integer.parseInt(st.nextToken());
int num2=Integer.parseInt(st.nextToken());
nodes[num1].add(num2);
nodes[num2].add(num1);
}
find(1);
for(int i=2; i<=N; i++) {
System.out.print(parent[i]+"\n");
}
}
private static void find(int nodeNum) {
visit[nodeNum]=true;
for(int i=0; i<nodes[nodeNum].size(); i++) {
int next= (int) nodes[nodeNum].get(i);
if(visit[next]==false) {
visit[next]=true;
parent[next]=nodeNum;
find(next);
}
}
}
}
'알고리즘 문제풀이 > 백준' 카테고리의 다른 글
[백준 6087] 레이저 통신(JAVA) (0) | 2020.04.03 |
---|---|
[백준 1072] 게임(JAVA) (2) | 2020.04.02 |
[백준 1405] 미친 로봇(JAVA/C++) (0) | 2020.03.30 |
[백준 9237] 이장님 초대(JAVA) (0) | 2020.03.29 |
[백준 16954] 움직이는 미로 탈출(JAVA) (0) | 2020.03.27 |
댓글