using System; using System.Collections.Generic; using System.Text; using System.Linq; class Program { public void Proc() { Reader.IsDebug = false; int pointCount = int.Parse(Reader.ReadLine()); List[] lineDic = new List[pointCount + 1]; this.TreeDic = new TreeNode[pointCount + 1]; for(int i=0; i(); this.TreeDic[inpt[0]] = new TreeNode(inpt[0]); } lineDic[inpt[0]].Add(inpt[1]); if(lineDic[inpt[1]] == null) { lineDic[inpt[1]] = new List(); this.TreeDic[inpt[1]] = new TreeNode(inpt[1]); } lineDic[inpt[1]].Add(inpt[0]); } this.SetTree(1, lineDic); Queue task = new Queue(); this.TreeDic.Where(a=>a.Items.Count == 0).ToList().ForEach(b=>task.Enqueue(b.ID)); while (task.Count > 0) { int key = task.Dequeue(); TreeNode target = TreeDic[key]; if(target.Parent != null && (target.Parent.FarFromLeaf == 0 || target.Parent.FarFromLeaf > target.FarFromLeaf + 1)) { target.Parent.FarFromLeaf = target.FarFromLeaf + 1; task.Enqueue(target.Parent.ID); } target.Items.ForEach((a)=>{ if((a.FarFromLeaf == 0 && a.Items.Count > 0) || a.FarFromLeaf > target.FarFromLeaf + 1) { a.FarFromLeaf = target.FarFromLeaf + 1; task.Enqueue(a.ID); } }); } this.TreeDic.OrderBy(a=>a.ID).ToList().ForEach(b=>Console.WriteLine(Math.Min(b.FarFromLeaf, b.FarFromRoot))); } private void SetTree(int parent, List[] dic) { dic[parent].ForEach((a)=>{ if(TreeDic[parent].Parent == null || a != TreeDic[parent].Parent.ID) { TreeDic[parent].Add(TreeDic[a]); } }); TreeDic[parent].Items.ForEach(a=>this.SetTree(a.ID, dic)); } TreeNode[] TreeDic; public class TreeNode { public int ID; public TreeNode Parent; public List Items = new List(); public int FarFromRoot; public int FarFromLeaf; public void Add(TreeNode child) { this.Items.Add(child); child.Parent = this; child.FarFromRoot = this.FarFromRoot + 1; child.RefreshFarFromRoot(); } private void RefreshFarFromRoot() { if(this.Parent == null) { this.FarFromRoot = 0; } else { this.FarFromRoot = this.Parent.FarFromRoot + 1; } if(this.Items.Count > 0) { this.Items.ForEach(a=>a.RefreshFarFromRoot()); } } public TreeNode(int id) { this.ID = id; } } public class Reader { public static bool IsDebug = true; private static String PlainInput = @" "; private static System.IO.StringReader Sr = null; public static string ReadLine() { if (IsDebug) { if (Sr == null) { Sr = new System.IO.StringReader(PlainInput.Trim()); } return Sr.ReadLine(); } else { return Console.ReadLine(); } } public static int[] GetInt(char delimiter = ' ', bool trim = false) { string inptStr = ReadLine(); if (trim) { inptStr = inptStr.Trim(); } string[] inpt = inptStr.Split(delimiter); int[] ret = new int[inpt.Length]; for (int i = 0; i < inpt.Length; i++) { ret[i] = int.Parse(inpt[i]); } return ret; } } static void Main() { Program prg = new Program(); prg.Proc(); } }