結果

問題 No.3694 犬猿の仲
コンテスト
ユーザー aketijyuuzou
提出日時 2026-09-09 21:38:46
言語 C#(csc)
(csc 3.9.0)
コンパイル:
csc -langversion:latest -unsafe -warn:0 -o+ /r:System.Numerics.dll _filename_ -out:a.exe
実行:
/usr/bin/mono a.exe
結果
AC  
実行時間 268 ms / 2,000 ms
+ 521µs
コード長 3,683 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 664 ms
コンパイル使用メモリ 109,824 KB
実行使用メモリ 71,012 KB
最終ジャッジ日時 2026-09-09 21:38:54
合計ジャッジ時間 3,254 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 11
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #
raw source code

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        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<int, List<int>> mToNodeListDict = new Dictionary<int, List<int>>();

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = { };
        Action<string> 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<int>();
            }
            if (mToNodeListDict.ContainsKey(ToNode) == false) {
                mToNodeListDict[ToNode] = new List<int>();
            }
            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<int, int> mLevelMod2Dict = new Dictionary<int, int>();

    struct JyoutaiDef
    {
        internal int CurrNode;
        internal int Level;
    }

    static bool ExecDFS(int pStaNode)
    {
        var Stk = new Stack<JyoutaiDef>();
        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;
    }
}

0