競技プログラミングの鉄則    次の問題へ    前の問題へ

B65 Road to Promotion Hard


問題へのリンク


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

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

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

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

        SplitAct(InputList[0]);
        long N = wkArr[0];
        long RootNode = wkArr[1];

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            long FromNode = wkArr[0];
            long ToNode = wkArr[1];

            if (mToNodeListDict.ContainsKey(FromNode) == false) {
                mToNodeListDict[FromNode] = new List<long>();
            }
            if (mToNodeListDict.ContainsKey(ToNode) == false) {
                mToNodeListDict[ToNode] = new List<long>();
            }
            mToNodeListDict[FromNode].Add(ToNode);
            mToNodeListDict[ToNode].Add(FromNode);
        }
        List<JyoutaiDef> DFSResult = ExecDFS(RootNode);
        DFSResult = DFSResult.OrderByDescending(pX => pX.Level).ToList();

        // 階級[社員]なDict
        var DPDict = new Dictionary<long, long>();
        for (long I = 1; I <= N; I++) {
            DPDict[I] = 0;
        }

        // DFSでのレベルの降順での、親ノードへの配る木DP
        foreach (JyoutaiDef EachJyoutai in DFSResult) {
            long CurrNode = EachJyoutai.Node;
            long ParentNode = EachJyoutai.ParentNode;
            if (ParentNode == -1) continue;

            DPDict[ParentNode] = Math.Max(DPDict[ParentNode], DPDict[CurrNode] + 1);
        }

        var Query = DPDict.OrderBy(pX => pX.Key).Select(pX => pX.Value);
        Console.WriteLine(LongEnumJoin(" ", Query));
    }

    // セパレータとLong型の列挙を引数として、結合したstringを返す
    static string LongEnumJoin(string pSeparater, IEnumerable<long> pEnum)
    {
        string[] StrArr = Array.ConvertAll(pEnum.ToArray(), pX => pX.ToString());
        return string.Join(pSeparater, StrArr);
    }

    struct JyoutaiDef
    {
        internal long Node;
        internal long ParentNode;
        internal long Level;
    }

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

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

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

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


解説

木DPで、DFSでのレベルの降順に配ってます。