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

ABC133-E Virus Tree 2


問題へのリンク


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

    const long Hou = 1000000007;

    static int mK;

    // 隣接リスト
    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]);
        mK = wkArr[1];

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

        DFS();
    }

    // 塗り分けの場合の数[ノード]
    static Dictionary<int, long> mPatternDict = new Dictionary<int, long>();

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

    static void DFS()
    {
        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrNode = 1;
        WillPush.Level = 1;
        mPatternDict[1] = mK;

        Stk.Push(WillPush);

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

            if (mToNodeListDict.ContainsKey(Popped.CurrNode)) {
                int CurrPattern = mK - Math.Min(2, Popped.Level);
                foreach (int EachToNode in mToNodeListDict[Popped.CurrNode]) {
                    if (mPatternDict.ContainsKey(EachToNode)) continue;

                    mPatternDict[EachToNode] = CurrPattern;
                    CurrPattern--;
                    if (CurrPattern < 0) CurrPattern = 0;

                    WillPush.CurrNode = EachToNode;
                    WillPush.Level = Popped.Level + 1;
                    Stk.Push(WillPush);
                }
            }
        }

        long Answer = 1;
        foreach (int EachInt in mPatternDict.Values) {
            Answer *= EachInt;
            Answer %= Hou;
        }
        Console.WriteLine(Answer);
    }
}


解説

根付き木として、DFSで根から順に、
ノードを訪問した時に色を塗ってます。

使える色は、
ノードのレベルが1ならK色
ノードのレベルが2ならK-1色
ノードのレベルが3以上ならK-2色
ですので、ノードごとに何通りの色が使えるかを調べてから
最後に積の法則を使ってます。