# Here your code ! INF = 10 ** 8 n = gets.to_i parent = Array.new(n+1,1) depth = Array.new(n+1,0) foot = Array.new(n+1,INF) leaf = Array.new(n+1,true) (n - 1).times do from, to = gets.split.map(&:to_i) parent[to] = from depth[to] = depth[from] + 1 leaf[from] = false end (1..n).select {|v| leaf[v]}.each do |i| d = depth[i] d.times.inject(i) do |j, dep| foot[j] = [foot[j], dep].min parent[j] end end puts (1..n).map {|i| [depth[i], foot[i]].min }