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

ABC222-E Red and Blue Tree


問題へのリンク


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

    static int mN;
    static int mK;
    static int[] mAArr;

    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];
        mK = wkArr[2];

        mAArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();

        foreach (string EachStr in InputList.Skip(2)) {
            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 (mToNodeListDict.ContainsKey(ToNode) == false) {
                mToNodeListDict[ToNode] = new List<int>();
            }
            mToNodeListDict[ToNode].Add(FromNode);
        }

        Solve();
    }

    const long Hou = 998244353;

    static void Solve()
    {
        for (int I = 1; I <= mAArr.GetUpperBound(0); I++) {
            int StaNode = mAArr[I - 1];
            int GoalNode = mAArr[I];

            ExecDFS(StaNode, GoalNode);
        }

        // 場合の数[R-B]なDP表
        var PrevDP = new Dictionary<long, long>();
        PrevDP[0] = 1;
        foreach (long EachUseCnt in mUseCntDict.Values) {
            var CurrDP = new Dictionary<long, long>();
            foreach (var EachDPPair in PrevDP) {

                Action<long> UpdateAct = pNewKey =>
                {
                    if (CurrDP.ContainsKey(pNewKey) == false) {
                        CurrDP[pNewKey] = 0;
                    }
                    CurrDP[pNewKey] += EachDPPair.Value;
                    CurrDP[pNewKey] %= Hou;
                };

                // Rにする経路
                UpdateAct(EachDPPair.Key + EachUseCnt);

                // Bにする経路
                UpdateAct(EachDPPair.Key - EachUseCnt);
            }
            PrevDP = CurrDP;
        }
        if (PrevDP.ContainsKey(mK)) {
            // 未使用の辺の分の組み合わせ数に、積の法則を使う
            long TotalEdgeCnt = mN - 1;
            long RestEdgeCnt = TotalEdgeCnt - mUseCntDict.Count;

            long Bekijyou2 = 1;
            for (long I = 1; I <= RestEdgeCnt; I++) {
                Bekijyou2 *= 2;
                Bekijyou2 %= Hou;
            }
            long Answer = (PrevDP[mK] * Bekijyou2) % Hou;
            Console.WriteLine(Answer);
        }
        else {
            Console.WriteLine(0);
        }
    }

    // 使用回数[辺のハッシュ値]なDict
    static Dictionary<long, int> mUseCntDict = new Dictionary<long, int>();

    struct JyoutaiDef
    {
        internal int CurrNode;
        internal int PrevNode;
        internal Dictionary<long, int> UseCntDict;
    }

    // 始点と終点を引数として、DFSで最短経路で使用する辺を調べる
    static void ExecDFS(int pStaNode, int pGoalNode)
    {
        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrNode = pStaNode;
        WillPush.PrevNode = -1;
        WillPush.UseCntDict = new Dictionary<long, int>();
        Stk.Push(WillPush);

        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();

            // クリア判定
            if (Popped.CurrNode == pGoalNode) {
                foreach (var EachPair in Popped.UseCntDict) {
                    if (mUseCntDict.ContainsKey(EachPair.Key) == false) {
                        mUseCntDict[EachPair.Key] = 0;
                    }
                    mUseCntDict[EachPair.Key] += EachPair.Value;
                }
                return;
            }

            if (mToNodeListDict.ContainsKey(Popped.CurrNode)) {
                foreach (int EachToNode in mToNodeListDict[Popped.CurrNode]) {
                    if (EachToNode == Popped.PrevNode) continue;
                    WillPush.CurrNode = EachToNode;
                    WillPush.PrevNode = Popped.CurrNode;
                    WillPush.UseCntDict = new Dictionary<long, int>(Popped.UseCntDict);
                    long EdgeHash = GetEdgeHash(Popped.CurrNode, EachToNode);
                    WillPush.UseCntDict[EdgeHash] = 1;
                    Stk.Push(WillPush);
                }
            }
        }
    }

    // 辺のハッシュ値を返す
    static long GetEdgeHash(int pFromNode, int pToNode)
    {
        long Min = Math.Min(pFromNode, pToNode);
        long Max = Math.Max(pFromNode, pToNode);

        return Max * 10000 + Min;
    }
}


解説

手順01 DFSを繰り返して、辺ごとに、何回使うかを求める
手順02 場合の数[R-B] で DP

で解いてます。