using System; using System.Collections.Generic; using System.Linq; class Program { static string InputPattern = "InputX"; static List GetInputList() { var WillReturn = new List(); if (InputPattern == "Input1") { WillReturn.Add("8 7"); WillReturn.Add("1 5"); WillReturn.Add("1 6"); WillReturn.Add("2 7"); WillReturn.Add("3 7"); WillReturn.Add("4 6"); WillReturn.Add("5 8"); WillReturn.Add("6 8"); //Yes } else if (InputPattern == "Input2") { WillReturn.Add("6 7"); WillReturn.Add("1 6"); WillReturn.Add("2 6"); WillReturn.Add("3 6"); WillReturn.Add("2 4"); WillReturn.Add("3 5"); WillReturn.Add("1 3"); WillReturn.Add("1 4"); //No } else { string wkStr; while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr); } return WillReturn; } static int mN; // 隣接リスト static Dictionary> mToNodeListDict = new Dictionary>(); static void Main() { List InputList = GetInputList(); int[] wkArr = { }; Action SplitAct = pStr => wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray(); SplitAct(InputList[0]); mN = wkArr[0]; foreach (string EachStr in InputList.Skip(1)) { SplitAct(EachStr); int FromNode = wkArr[0]; int ToNode = wkArr[1]; if (mToNodeListDict.ContainsKey(FromNode) == false) { mToNodeListDict[FromNode] = new List(); } if (mToNodeListDict.ContainsKey(ToNode) == false) { mToNodeListDict[ToNode] = new List(); } mToNodeListDict[FromNode].Add(ToNode); mToNodeListDict[ToNode].Add(FromNode); } for (int I = 1; I <= mN; I++) { if (mLevelMod2Dict.ContainsKey(I)) { continue; } bool Result = ExecDFS(I); if (Result == false) { Console.WriteLine("No"); return; } } Console.WriteLine("Yes"); } static Dictionary mLevelMod2Dict = new Dictionary(); struct JyoutaiDef { internal int CurrNode; internal int Level; } static bool ExecDFS(int pStaNode) { var Stk = new Stack(); JyoutaiDef WillPush; WillPush.CurrNode = pStaNode; WillPush.Level = 1; Stk.Push(WillPush); while (Stk.Count > 0) { JyoutaiDef Popped = Stk.Pop(); // 訪問済なら矛盾がないかのチェックだけ行う if (mLevelMod2Dict.ContainsKey(Popped.CurrNode)) { int CorrectLevelMod2 = mLevelMod2Dict[Popped.CurrNode]; if (CorrectLevelMod2 != Popped.Level % 2) { return false; } continue; } // 未訪問なら採色 mLevelMod2Dict[Popped.CurrNode] = Popped.Level % 2; if (mToNodeListDict.ContainsKey(Popped.CurrNode) == false) { continue; } foreach (int EachToNode in mToNodeListDict[Popped.CurrNode]) { WillPush.CurrNode = EachToNode; WillPush.Level = Popped.Level + 1; Stk.Push(WillPush); } } return true; } }