# frozen_string_literal: true n = gets.to_i edges = Array.new(n - 1) { gets.split.map(&:to_i) } root_node = 1 leaf_nodes = edges.flatten .group_by(&:itself) .find_all { |_k, v| v.length == 1 } .map(&:first) - [root_node] root_lengths = Array.new(n + 1) checked_nodes = Array.new(n + 1) { false } queue = [] queue << 1 checked_nodes[1] = true root_lengths[1] = 0 while (i = queue.shift) next_root_length = root_lengths[i] + 1 edges.each do |x, y| if x == i t = y elsif y == i t = x else next end next if checked_nodes[t] queue << t checked_nodes[t] = true root_lengths[t] = next_root_length end end leaf_nodes.each do |l| checked_nodes = Array.new(n + 1) { false } leaf_lengths = Array.new(n + 1) queue = [] queue << l checked_nodes[l] = true leaf_lengths[l] = 0 root_lengths[l] = 0 while (i = queue.shift) next_leaf_length = leaf_lengths[i] + 1 edges.each do |x, y| if x == i t = y elsif y == i t = x else next end next if checked_nodes[t] checked_nodes[t] = true next if root_lengths[t] < next_leaf_length queue << t leaf_lengths[t] = next_leaf_length root_lengths[t] = next_leaf_length end end end root_lengths.shift puts root_lengths