典型90問    次の典型90問へ    前の典型90問へ

典型90問 073 We Need Both a and b(★5)


問題へのリンク


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("b a b a b b a");
            WillReturn.Add("2 1");
            WillReturn.Add("3 7");
            WillReturn.Add("3 2");
            WillReturn.Add("3 4");
            WillReturn.Add("5 4");
            WillReturn.Add("4 6");
            //4
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("2");
            WillReturn.Add("a b");
            WillReturn.Add("1 2");
            //1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("22");
            WillReturn.Add("b a b b a b b b a b a a a a b b a b b a a a");
            WillReturn.Add("1 7");
            WillReturn.Add("4 14");
            WillReturn.Add("12 22");
            WillReturn.Add("2 4");
            WillReturn.Add("21 17");
            WillReturn.Add("3 20");
            WillReturn.Add("7 8");
            WillReturn.Add("20 14");
            WillReturn.Add("15 11");
            WillReturn.Add("8 14");
            WillReturn.Add("9 12");
            WillReturn.Add("17 8");
            WillReturn.Add("6 20");
            WillReturn.Add("11 20");
            WillReturn.Add("18 19");
            WillReturn.Add("10 8");
            WillReturn.Add("22 20");
            WillReturn.Add("13 21");
            WillReturn.Add("5 14");
            WillReturn.Add("19 20");
            WillReturn.Add("16 14");
            //16
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    const int Hou = 1000000007;

    static int mN;
    static char[] mCArr;

    // 隣接リスト
    static Dictionary<int, List<int>> mToNodeListDict = new Dictionary<int, List<int>>();

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

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

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

        List<JyoutaiDef> DFSResult = ExecDFS();
        DFSResult.Reverse();

        // 赤黒の有無のBitSetの値
        // 1 aだけ
        // 2 bだけ
        // 3 aとbの両方 

        // 場合の数[ ノード , 赤黒の有無のBitSet ] なDP表
        long[,] DPArr = new long[mN + 1, 3 + 1];
        for (int I = 1; I <= mN; I++) {
            char CurrColor = mCArr[I - 1];
            if (CurrColor == 'a') DPArr[I, 1] = 1;
            if (CurrColor == 'b') DPArr[I, 2] = 1;
        }

        foreach (JyoutaiDef EachJyoutai in DFSResult) {
            int ChildNode = EachJyoutai.Node;
            int ParentNode = EachJyoutai.ParentNode;
            if (ParentNode == -1) continue;

            // 子から親に配るDP
            long[] PatternArr = new long[3 + 1];

            for (int I = 1; I <= 3; I++) {
                if (DPArr[ChildNode, I] == 0) continue;

                for (int J = 1; J <= 3; J++) {
                    if (DPArr[ParentNode, J] == 0) continue;

                    // 枝を切断して配る場合
                    if (I == 3) {
                        PatternArr[J] += DPArr[ChildNode, I] * DPArr[ParentNode, J]; // 積の法則
                        PatternArr[J] %= Hou;
                    }

                    // 枝を切断しないで配る場合
                    int BitOR = I | J;
                    PatternArr[BitOR] += DPArr[ChildNode, I] * DPArr[ParentNode, J]; // 積の法則
                    PatternArr[J] %= Hou;
                }
            }
            DPArr[ParentNode, 1] = PatternArr[1];
            DPArr[ParentNode, 2] = PatternArr[2];
            DPArr[ParentNode, 3] = PatternArr[3];
        }
        Console.WriteLine(DPArr[1, 3]);
    }

    struct JyoutaiDef
    {
        internal int Node;
        internal int ParentNode;
    }

    static List<JyoutaiDef> ExecDFS()
    {
        var WillReturn = new List<JyoutaiDef>();

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.Node = 1;
        WillPush.ParentNode = -1;
        Stk.Push(WillPush);

        var VisitedSet = new HashSet<int>();
        VisitedSet.Add(1);

        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();
            WillReturn.Add(Popped);
            if (mToNodeListDict.ContainsKey(Popped.Node)) {
                foreach (int EachToNode in mToNodeListDict[Popped.Node]) {
                    if (VisitedSet.Add(EachToNode)) {
                        WillPush.Node = EachToNode;
                        WillPush.ParentNode = Popped.Node;
                        Stk.Push(WillPush);
                    }
                }
            }
        }
        return WillReturn;
    }
}


解説

木DPで解いてます。