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

ABC309-E Family and Insurance


問題へのリンク


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

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] PArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        var MaxValDict = new Dictionary<int, int>();
        foreach (string EachStr in InputList.Skip(2)) {
            SplitAct(EachStr);

            int X = wkArr[0];
            int Y = wkArr[1];
            if (MaxValDict.ContainsKey(X) == false) {
                MaxValDict[X] = Y;
            }
            MaxValDict[X] = Math.Max(MaxValDict[X], Y);
        }

        var ChildNodeListDict = new Dictionary<int, List<int>>();
        for (int I = 0; I <= PArr.GetUpperBound(0); I++) {
            int Parent = PArr[I];
            if (ChildNodeListDict.ContainsKey(Parent) == false) {
                ChildNodeListDict[Parent] = new List<int>();
            }
            ChildNodeListDict[Parent].Add(I + 2);
        }

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrNode = 1;
        WillPush.RestVal = -1;
        if (MaxValDict.ContainsKey(1)) {
            WillPush.RestVal = MaxValDict[1];
        }
        Stk.Push(WillPush);

        var AnswerList = new List<int>();

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

            if (Popped.RestVal >= 0) {
                AnswerList.Add(Popped.CurrNode);
            }
            Popped.RestVal--;

            if (ChildNodeListDict.ContainsKey(Popped.CurrNode)) {
                foreach (int EachChild in ChildNodeListDict[Popped.CurrNode]) {
                    int RestVal = Popped.RestVal;
                    if (MaxValDict.ContainsKey(EachChild)) {
                        RestVal = Math.Max(RestVal, MaxValDict[EachChild]);
                    }
                    WillPush.CurrNode = EachChild;
                    WillPush.RestVal = RestVal;
                    Stk.Push(WillPush);
                }
            }
        }
        Console.WriteLine(AnswerList.Count);
    }

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


解説

保険適用数の残りを状態に持って、
木の根からDFSしてます。