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()); Dictionary> lineDic = new Dictionary>(); for(int i=0; i()); } lineDic[inpt[0]].Add(inpt[1]); if(!lineDic.ContainsKey(inpt[1])) { lineDic.Add(inpt[1], new List()); } lineDic[inpt[1]].Add(inpt[0]); } lineDic.ToList().ForEach(a=>this.TreeDic.Add(a.Key, new TreeNode(a.Key))); this.SetTree(1, lineDic); Queue task = new Queue(); this.TreeDic.Where(a=>a.Value.Items.Count == 0).ToList().ForEach(b=>task.Enqueue(b.Key)); 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.Key).ToList().ForEach(b=>Console.WriteLine(Math.Min(b.Value.FarFromLeaf, b.Value.FarFromRoot))); } private void SetTree(int parent, Dictionary>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)); } Dictionary TreeDic = new Dictionary(); 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(); } }