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

ABC021-C 正直者の高橋くん


問題へのリンク


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

    const long Hou = 1000000007;

    // 隣接リスト
    static Dictionary<int, List<int>> mToNodeArrDict = 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[1]);
        int a = wkArr[0];
        int b = wkArr[1];

        foreach (string EachStr in InputList.Skip(3)) {
            SplitAct(EachStr);

            int FromNode = wkArr[0];
            int ToNode = wkArr[1];

            if (mToNodeArrDict.ContainsKey(FromNode) == false) {
                mToNodeArrDict[FromNode] = new List<int>();
            }
            if (mToNodeArrDict.ContainsKey(ToNode) == false) {
                mToNodeArrDict[ToNode] = new List<int>();
            }
            mToNodeArrDict[FromNode].Add(ToNode);
            mToNodeArrDict[ToNode].Add(FromNode);
        }

        // 場合の数[現在ノード]なDP表
        var PrevDPDict = new Dictionary<int, long>();
        PrevDPDict[a] = 1;

        var VisitedSet = new HashSet<int>();
        VisitedSet.Add(a);
        while (true) {
            if (PrevDPDict.ContainsKey(b)) {
                break;
            }
            var CurrDPDict = new Dictionary<int, long>();
            foreach (var EachPair in PrevDPDict) {
                if (mToNodeArrDict.ContainsKey(EachPair.Key) == false) {
                    continue;
                }
                foreach (int EachToNode in mToNodeArrDict[EachPair.Key]) {
                    if (VisitedSet.Contains(EachToNode)) continue;

                    if (CurrDPDict.ContainsKey(EachToNode) == false) {
                        CurrDPDict[EachToNode] = 0;
                    }
                    CurrDPDict[EachToNode] += EachPair.Value;
                    CurrDPDict[EachToNode] %= Hou;
                }
            }
            foreach (int EachNode in CurrDPDict.Keys) {
                VisitedSet.Add(EachNode);
            }
            PrevDPDict = CurrDPDict;

            //Console.WriteLine("DP結果");
            //foreach (var EachPair in PrevDPDict) {
            //    Console.WriteLine("PrevDPDict[{0}]={1}", EachPair.Key, EachPair.Value);
            //}
        }
        Console.WriteLine(PrevDPDict[b]);
    }
}


解説

幅優先探索チックな木DPで解いてます。


類題

ABC211-D Number of Shortest paths