import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.Scanner; import java.util.Set; public class Main { public static int INF = Integer.MAX_VALUE / 2 - 1; public static int[] bfs(final int N, ArrayList> adj, Set starts){ int[] costs = new int[N]; Arrays.fill(costs, INF); LinkedList node_queue = new LinkedList(); boolean[] in_queue = new boolean[N]; for(final int start : starts){ costs[start] = 0; node_queue.add(start); in_queue[start] = true; } while(!node_queue.isEmpty()){ final int node = node_queue.poll(); in_queue[node] = false; for(final int next : adj.get(node)){ final int next_cost = costs[node] + 1; if(next_cost >= costs[next]){ continue; } costs[next] = next_cost; if(!in_queue[next]){ node_queue.add(next); } } } return costs; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int N = sc.nextInt(); ArrayList> adj = new ArrayList>(); for(int i = 0; i < N; i++){ adj.add(new LinkedHashSet()); } int[] degrees = new int[N]; for(int i = 0; i < N - 1; i++){ final int x = sc.nextInt() - 1; final int y = sc.nextInt() - 1; degrees[x]++; degrees[y]++; adj.get(x).add(y); adj.get(y).add(x); } Set leafs = new HashSet(); for(int i = 0; i < N; i++){ if(degrees[i] == 1){ leafs.add(i); } } int[] leaf_dist = bfs(N, adj, leafs); for(int i = 0; i < N; i++){ System.out.println(leaf_dist[i]); } } }