using System; using System.Linq; using System.Collections.Generic; using System.Text.RegularExpressions; using System.Text; public class Program { public void Proc() { Reader.IsDebug =false; int itemCount = int.Parse(Reader.ReadLine()); Dictionary pList = new Dictionary(); for(int i=0; i{ if(pList.ContainsKey(a)) { pList[a] = true; } else { pList.Add(a, false); } }); } pList.Where(a=>a.Value).Select(a=>a.Key).ToList().ForEach(a=>{ PosList.Add(PosList.Count, a); KeyList.Add(a, KeyList.Count); }); this.Target = this.MatchList.Where(a=>pList[a.P1] || pList[a.P2]).ToArray(); bool ans = this.GetAns(0, new bool[PosList.Count]); Console.WriteLine(ans?"YES":"NO"); } private Dictionary> dic = new Dictionary>(); private bool GetAns(int idx, bool[] flags) { string key = string.Join(string.Empty, flags.Select(a=>a?1:0)); if(!dic.ContainsKey(idx)) { dic.Add(idx, new Dictionary()); } if(dic[idx].ContainsKey(key)) { return dic[idx][key]; } if(idx >= Target.Count()) { return true; } Pos p1 = this.Target[idx].P1; Pos p2 = this.Target[idx].P2; if(KeyList.ContainsKey(p1)) { if(!flags[KeyList[p1]]) { bool[] sub = (bool[])flags.Clone(); sub[KeyList[p1]] = true; bool ret = this.GetAns(idx+1, sub); if(ret) { return true; } } } else { bool[] sub = (bool[])flags.Clone(); bool ret = this.GetAns(idx+1, sub); if(ret) { return true; } } if(KeyList.ContainsKey(p2)) { if(!flags[KeyList[p2]]) { bool[] sub = (bool[])flags.Clone(); sub[KeyList[p2]] = true; bool ret = this.GetAns(idx+1, sub); if(ret) { return true; } } } else { bool[] sub = (bool[])flags.Clone(); bool ret = this.GetAns(idx+1, sub); if(ret) { return true; } } dic[idx][key] = false; return false; } private Dictionary KeyList = new Dictionary(); private Dictionary PosList = new Dictionary(); private Match[] Target; private List MatchList = new List(); private class Match { public Pos P1; public Pos P2; public Match(string init) { int[] inpt = init.Split(' ').Select(a=>int.Parse(a)).ToArray(); this.P1 = new Pos(inpt[0], inpt[1]); this.P2 = new Pos(inpt[2], inpt[3]); } } private struct Pos { public int Row; public int Col; public Pos(int r, int c) { this.Row = r; this.Col = c; } } public class Reader { public static bool IsDebug = true; private static System.IO.StringReader SReader; private static string InitText = @" 2 1 1 2 1 2 1 3 1 "; 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(); } }