AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC376-D Cycle


問題へのリンク


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 3");
            WillReturn.Add("3 1");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 2");
            WillReturn.Add("1 2");
            WillReturn.Add("2 3");
            //-1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("6 9");
            WillReturn.Add("6 1");
            WillReturn.Add("1 5");
            WillReturn.Add("2 6");
            WillReturn.Add("2 1");
            WillReturn.Add("3 6");
            WillReturn.Add("4 2");
            WillReturn.Add("6 4");
            WillReturn.Add("3 5");
            WillReturn.Add("5 4");
            //4
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    // 隣接リスト
    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]);

        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);
        }

        var Que = new Queue<JyoutaiDef>();
        JyoutaiDef WillEnqueue;
        WillEnqueue.CurrNode = 1;
        WillEnqueue.Level = 0;
        Que.Enqueue(WillEnqueue);

        var VisitedSet = new HashSet<int>();
        while (Que.Count > 0) {
            JyoutaiDef Dequeued = Que.Dequeue();

            if (mToNodeListDict.ContainsKey(Dequeued.CurrNode)) {
                foreach (int EachToNode in mToNodeListDict[Dequeued.CurrNode]) {
                    if (EachToNode == 1) {
                        Console.WriteLine(Dequeued.Level + 1);
                        return;
                    }

                    if (VisitedSet.Add(EachToNode)) {
                        WillEnqueue.CurrNode = EachToNode;
                        WillEnqueue.Level = Dequeued.Level + 1;
                        Que.Enqueue(WillEnqueue);
                    }
                }
            }
        }
        Console.WriteLine(-1);
    }

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


解説

考察すると、最訪防止のBFSで良いと分かります。