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

ABC428-E Farthest Vertex


問題へのリンク


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

    static long[] GetSplitArr(string pStr)
    {
        return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
    }

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

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

        long NodeCnt = long.Parse(InputList[0]);

        long[] wkArr = { };
        Action<string> SplitAct = (pStr) => wkArr = GetSplitArr(pStr);

        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);
        }

        // 直径の葉ノード1を求める
        Dictionary<long, long> LevelDict1 = ClassTreeDFS.ExecTreeDFS(1, mToNodeListDict);
        long Leaf1 = LevelDict1.OrderByDescending(pX => pX.Value).ThenByDescending(pX => pX.Key).First().Key;

        // 直径の葉ノード2を求める
        Dictionary<long, long> LevelDict2 = ClassTreeDFS.ExecTreeDFS(Leaf1, mToNodeListDict);
        long Leaf2 = LevelDict2.OrderByDescending(pX => pX.Value).ThenByDescending(pX => pX.Key).First().Key;

        // 葉ノード1からの距離[ノード]なDictを作成
        Dictionary<long, long> LevelDict3 = LevelDict2;

        // 葉ノード2からの距離[ノード]なDictを作成
        Dictionary<long, long> LevelDict4 = ClassTreeDFS.ExecTreeDFS(Leaf2, mToNodeListDict);

        var sb = new System.Text.StringBuilder();
        for (long I = 1; I <= NodeCnt; I++) {
            long Level1 = LevelDict3[I];
            long Level2 = LevelDict4[I];
            if (Level1 > Level2) {
                sb.Append(Leaf1);
            }
            else if (Level1 < Level2) {
                sb.Append(Leaf2);
            }
            else {
                sb.Append(Math.Max(Leaf1, Leaf2));
            }
            sb.AppendLine();
        }
        Console.Write(sb.ToString());
    }
}

#region ClassTreeDFS
// 根付き木のDFSクラス
internal class ClassTreeDFS
{
    // ルートノードと連結リストを引数とし、レベル[ノード]のDictを返す
    static internal Dictionary<long, long> ExecTreeDFS(
        long pRootNode, Dictionary<long, List<long>> pToNodeListDict)
    {
        var LevelDict = new Dictionary<long, long>();

        var Stk = new Stack<JyoutaiDefTreeDFS>();
        JyoutaiDefTreeDFS WillPush;
        WillPush.CurrNode = pRootNode;
        WillPush.Level = 1;
        Stk.Push(WillPush);

        var VisitedSet = new HashSet<long>();
        VisitedSet.Add(pRootNode);
        while (Stk.Count > 0) {
            JyoutaiDefTreeDFS Popped = Stk.Pop();

            LevelDict[Popped.CurrNode] = Popped.Level;

            foreach (long EachToNode in pToNodeListDict[Popped.CurrNode]) {
                if (VisitedSet.Add(EachToNode)) {
                    WillPush.CurrNode = EachToNode;
                    WillPush.Level = Popped.Level + 1;
                    Stk.Push(WillPush);
                }
            }
        }
        return LevelDict;
    }

    private struct JyoutaiDefTreeDFS
    {
        internal long CurrNode;
        internal long Level;
    }
}
#endregion


解説

まず、double sweepのアルゴリズムで木の直径を求めます。
それからノードごとに、直径の2つの葉ノードのどっちが解かを判定してます。