using System; using System.Linq; using System.Text; using System.Collections.Generic; public class Program { public void Proc() { Reader.IsDebug = false; int[] inpt= Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray(); this.ShimaCount = inpt[0]; this.BridgeCount = inpt[1]; int eventCount = inpt[2]; for(int i=0;iint.Parse(a)).ToArray(); int[][] posList = new int[][] {inpt.OrderBy(a=>a).ToArray(), inpt.OrderByDescending(a=>a).ToArray() }; foreach(int[] pos in posList) { if(!this.BridgeDic.ContainsKey(pos[0])) { this.BridgeDic.Add(pos[0], new Dictionary()); } this.BridgeDic[pos[0]][pos[1]] = 0; } } for(int i=0; iint.Parse(a)).ToArray(); this.BridgeDic[inpt[0]][inpt[1]] = i+1; this.BridgeDic[inpt[1]][inpt[0]] = i+1; this.EventList.Add(inpt); } Dictionary ShimaGroup = new Dictionary(); Dictionary> GroupShima = new Dictionary>(); Dictionary GroupRef = new Dictionary(); Dictionary ans = new Dictionary(); for(int i=1; i<=this.ShimaCount; i++) { ans.Add(i, 0); if(ShimaGroup.ContainsKey(i)) { continue; } GroupRef.Add(i, i); if(!BridgeDic.ContainsKey(i)) { continue; } GroupShima.Add(i, new List()); Queue q = new Queue(); q.Enqueue(i); while(q.Count > 0) { int cur = q.Dequeue(); if(ShimaGroup.ContainsKey(cur)) { continue; } if(!BridgeDic.ContainsKey(cur)) { continue; } ShimaGroup.Add(cur, i); GroupShima[i].Add(cur); this.BridgeDic[cur].Where(a=>a.Value == 0).ToList().ForEach(a=>q.Enqueue(a.Key)); } } ShimaGroup.Where(a=>a.Value == 1).ToList().ForEach(a=>ans[a.Key] = -1); for(int i=EventList.Count-1; i>=0; i--) { int[] grp = EventList[i].Select(a=>ShimaGroup[a]).OrderBy(a=>GroupRef[a]).ToArray(); int ref1 = GroupRef[grp[0]]; int ref2 = GroupRef[grp[1]]; if(ref1 == ref2) { continue; } int[] target = GroupRef.Where(a=>a.Value == ref2).Select(a=>a.Key).ToArray(); target.ToList().ForEach(a=>GroupRef[a] = ref1); if(ref1 == 1) { target.ToList().ForEach(a=>GroupShima[a].ToList().ForEach(b=>ans[b] = i+1)); } } ans.Remove(1); ans.ToList().ForEach(a=>Console.WriteLine(a.Value)); } private Dictionary> BridgeDic = new Dictionary>(); private List EventList = new List(); private int ShimaCount; private int BridgeCount; public class Reader { public static bool IsDebug = true; private static System.IO.StringReader SReader; private static string InitText = @" 10 15 11 1 2 1 3 1 6 2 5 2 9 2 10 3 4 4 6 5 7 5 9 6 7 6 9 8 9 8 10 9 10 1 2 1 3 4 6 5 9 9 10 2 5 6 7 8 9 8 10 3 4 2 9 "; public static string ReadLine() { if(IsDebug) { if(SReader == null) { SReader = new System.IO.StringReader(InitText.Trim()); } return SReader.ReadLine(); } else { return Console.ReadLine(); } } } public static void Main(string[] args) { Program prg = new Program(); prg.Proc(); } }