AtCoderのARC    次のARCの問題へ    前のARCの問題へ

ARC022-C ロミオとジュリエット


問題へのリンク


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("10");
            WillReturn.Add("7 6");
            WillReturn.Add("3 2");
            WillReturn.Add("2 4");
            WillReturn.Add("4 5");
            WillReturn.Add("8 9");
            WillReturn.Add("1 8");
            WillReturn.Add("1 6");
            WillReturn.Add("1 2");
            WillReturn.Add("9 10");
            //5 10
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4");
            WillReturn.Add("1 2");
            WillReturn.Add("1 3");
            WillReturn.Add("1 4");
            //4 3
        }
        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[] 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);
        }

        long Diameter, Leaf1, Leaf2;
        List<long> CenterNodeList;
        ClassTreeDiameter.DeriveTreeDiameter(1, mToNodeListDict,
            out Diameter, out Leaf1, out Leaf2, out CenterNodeList);

        Console.WriteLine("{0} {1}", Leaf1, Leaf2);
    }
}

#region ClassTreeDiameter
// 根付き木の直径を求めるクラス
internal class ClassTreeDiameter
{
    // {ルートノード,連結リスト}を引数とし、
    // {木の直径、葉ノード1、葉ノード2、木の中心のノードのList}を設定
    static internal void DeriveTreeDiameter(
        long pRootNode, Dictionary<long, List<long>> pToNodeListDict,
        out long pDiameter, out long pLeaf1, out long pLeaf2,
        out List<long> pCenterNodeList)
    {
        // ===============================================================
        // double sweepのアルゴリズム
        // ===============================================================

        // 根ノードからの距離[ノード]なDict
        var DistanceDictRoot = ExecDFS(1, pToNodeListDict);
        pLeaf1 = DistanceDictRoot.OrderByDescending(pX => pX.Value).First().Key;

        // 葉ノード1からの距離[ノード]なDict
        var DistanceDictLeaf1 = ExecDFS(pLeaf1, pToNodeListDict);
        pLeaf2 = DistanceDictLeaf1.OrderByDescending(pX => pX.Value).First().Key;

        pDiameter = DistanceDictLeaf1.Values.Max();

        pCenterNodeList = new List<long>();

        // 葉ノード2からの距離[ノード]なDict
        var DistanceDictLeaf2 = ExecDFS(pLeaf2, pToNodeListDict);

        var OKDistanceSet = new HashSet<long>();
        OKDistanceSet.Add(pDiameter / 2);
        if (pDiameter % 2 == 1) {
            OKDistanceSet.Add(pDiameter / 2 + 1);
        }

        var CenterNodeSet1 = new HashSet<long>();
        foreach (var EachPair in DistanceDictLeaf1) {
            if (OKDistanceSet.Contains(EachPair.Value)) {
                CenterNodeSet1.Add(EachPair.Key);
            }
        }

        var CenterNodeSet2 = new HashSet<long>();
        foreach (var EachPair in DistanceDictLeaf2) {
            if (OKDistanceSet.Contains(EachPair.Value)) {
                CenterNodeSet2.Add(EachPair.Key);
            }
        }

        CenterNodeSet1.IntersectWith(CenterNodeSet2);
        pCenterNodeList.AddRange(CenterNodeSet1);
    }

    // 根ノードを引数としてDFSを行う
    static private Dictionary<long, long> ExecDFS(long pRootNode, Dictionary<long, List<long>> pToNodeListDict)
    {
        var DistanceDict = new Dictionary<long, long>();

        var Stk = new Stack<JyoutaiDefTreeDiameter>();
        JyoutaiDefTreeDiameter WillPush;
        WillPush.CurrNode = pRootNode;
        WillPush.Distance = 0; // 距離なので0始まり
        Stk.Push(WillPush);

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

            DistanceDict[Popped.CurrNode] = Popped.Distance;

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

    private struct JyoutaiDefTreeDiameter
    {
        internal long CurrNode;
        internal long Distance;
    }
}
#endregion


解説

Nノードの無向グラフですが、
辺が(N-1)本で、連結なため
自己ループも多重辺もないことから
グラフは木だと分かります。

よって、木の直径を求めるアルゴリズムで解けると分かります。