[伯俊](ツリー)-ツリーの親を検索


https://www.acmicpc.net/problem/11725
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class TreeParent {
	
	
	public static void findParent(boolean[] visited, ArrayList<ArrayList<Integer>> ar,  int c, int []parent) {
		visited[c] = true;
		
		for(int i : ar.get(c)) {
			if (!visited[i]) {
				parent[i] = c;
				findParent(visited, ar, i, parent);
			}
		}
	}
	
	public static void main(String[] args) throws IOException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("src/tree_parent.txt"))));
//		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		
		int n = Integer.parseInt(st.nextToken());
		
		ArrayList<ArrayList<Integer>> ar = new ArrayList<ArrayList<Integer>>();
		for(int i=0; i<=n; i++) {
			ar.add(new ArrayList<Integer>());
		}
		
		for(int i=0; i<n-1; i++) {
			st = new StringTokenizer(br.readLine());
			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());
			ar.get(a).add(b);
			ar.get(b).add(a);
		}
		
		boolean visited[] = new boolean[n+1];
		int parent[] = new int[n+1];
				
		findParent(visited, ar, 1, parent);
		
		for(int i=2; i<=n; i++) {
			System.out.println(parent[i]);
		}
				
	}
}