AtCoderのPAST    次のPASTの問題へ    前のPASTの問題へ

第7回PAST J 終わりなき旅


問題へのリンク


C#のソース

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("3 3");
            WillReturn.Add("1 2");
            WillReturn.Add("2 1");
            WillReturn.Add("2 3");
            //Yes
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("8 7");
            WillReturn.Add("2 4");
            WillReturn.Add("2 8");
            WillReturn.Add("7 3");
            WillReturn.Add("6 1");
            WillReturn.Add("8 4");
            WillReturn.Add("8 3");
            WillReturn.Add("5 3");
            //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>();
            }
            mToNodeListDict[FromNode].Add(ToNode);
        }

        if (HasCycle(mN)) {
            Console.WriteLine("Yes");
        }
        else {
            Console.WriteLine("No");
        }
    }

    ////////////////////////////////////////////////////////////////
    // 有向グラフで閉路判定を行う
    ////////////////////////////////////////////////////////////////
    static bool HasCycle(int pNodeCnt)
    {
        // 入次数を配列で管理
        int[] DegreeArr = new int[pNodeCnt + 1];
        foreach (var EachPair in mToNodeListDict) {
            foreach (int EachToNode in EachPair.Value) {
                DegreeArr[EachToNode]++;
            }
        }

        var VisitedSet = new HashSet<int>();

        // 入次数が0で、未訪問ノードから深さ優先探索
        for (int I = 1; I <= pNodeCnt; I++) {
            if (DegreeArr[I] > 0) continue;
            if (VisitedSet.Contains(I)) continue;

            HasCycleHelperExecDFS(I, DegreeArr, VisitedSet);
        }

        return pNodeCnt > VisitedSet.Count;
    }

    // HasCycleのヘルパメソッドで、深さ優先探索を行う
    struct JyoutaiDef_HasCycle
    {
        internal int CurrNode;
    }
    static void HasCycleHelperExecDFS(int pStaNode, int[] pDegreeArr, HashSet<int> pVisitedSet)
    {
        JyoutaiDef_HasCycle WillPush;
        WillPush.CurrNode = pStaNode;

        var Stk = new Stack<JyoutaiDef_HasCycle>();
        Stk.Push(WillPush);
        while (Stk.Count > 0) {
            JyoutaiDef_HasCycle Popped = Stk.Pop();

            int CurrNode = Popped.CurrNode;
            pVisitedSet.Add(CurrNode);

            if (mToNodeListDict.ContainsKey(CurrNode) == false) {
                continue;
            }
            foreach (int EachToNode in mToNodeListDict[CurrNode]) {
                // 入次数はデクリメント
                pDegreeArr[EachToNode]--;

                // 入次数が0でなかったら訪問しない
                if (pDegreeArr[EachToNode] > 0) continue;

                // 再訪防止
                if (pVisitedSet.Add(EachToNode) == false) continue;

                WillPush.CurrNode = EachToNode;
                Stk.Push(WillPush);
            }
        }
    }
}


解説

有向グラフでCycleがないこと ⇔ トポロジカルソート可能なこと
なので、DFSでトポロジカルソートし、
トポロジカルソートの結果でCycleの有無を判定してます。