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

ABC239-E Subtree K-th Max


問題へのリンク


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

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

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

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

        SplitAct(InputList[0]);
        int N = wkArr[0];

        foreach (string EachStr in InputList.Skip(2).Take(N - 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);
        }

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

        var ChildListDict = new Dictionary<int, List<int>>();
        foreach (JyoutaiDef EachJyoutai in DFSResult) {
            if (ChildListDict.ContainsKey(EachJyoutai.Node) == false) {
                ChildListDict[EachJyoutai.Node] = new List<int>();
            }
            if (ChildListDict.ContainsKey(EachJyoutai.ParentNode) == false) {
                ChildListDict[EachJyoutai.ParentNode] = new List<int>();
            }
            ChildListDict[EachJyoutai.Node].Add(XArr[EachJyoutai.Node - 1]);
            ChildListDict[EachJyoutai.ParentNode].AddRange(ChildListDict[EachJyoutai.Node]);

            ChildListDict[EachJyoutai.Node] =
                ChildListDict[EachJyoutai.Node].OrderByDescending(pX => pX).Take(20).ToList();
            ChildListDict[EachJyoutai.ParentNode] =
                ChildListDict[EachJyoutai.ParentNode].OrderByDescending(pX => pX).Take(20).ToList();
        }

        var sb = new System.Text.StringBuilder();
        foreach (string EachStr in InputList.Skip(2 + N - 1)) {
            SplitAct(EachStr);
            int V = wkArr[0];
            int K = wkArr[1];

            sb.Append(ChildListDict[V][K - 1]);
            sb.AppendLine();
        }
        Console.Write(sb.ToString());
    }

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

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

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

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

        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してます。