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

ABC372-E K-th Largest Connected Components


問題へのリンク


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

    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 NodeCnt = wkArr[0];

        var InsUnionFindNodeList = new UnionFindNodeList();
        for (long I = 1; I <= NodeCnt; I++) {
            InsUnionFindNodeList.MakeSet(I);
        }

        var sb = new System.Text.StringBuilder();
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            long Type = wkArr[0];

            if (Type == 1) {
                long FromNode = wkArr[1];
                long ToNode = wkArr[2];
                InsUnionFindNodeList.Unite(FromNode, ToNode);
            }
            if (Type == 2) {
                long TargetNode = wkArr[1];
                long Rank = wkArr[2];

                long Answer = InsUnionFindNodeList.GetAnswer(TargetNode, Rank);
                sb.Append(Answer);
                sb.AppendLine();
            }
        }
        Console.Write(sb.ToString());
    }
}

// UnionFindNodeListクラス
internal class UnionFindNodeList
{
    private class NodeInfoDef
    {
        internal long ParentNode;
        internal long Rank;
        internal List<long> NodeList;
    }
    private Dictionary<long, NodeInfoDef> mNodeInfoDict =
        new Dictionary<long, NodeInfoDef>();

    // 要素が1つである木を森に追加
    internal void MakeSet(long pNode)
    {
        NodeInfoDef WillAdd = new NodeInfoDef();
        WillAdd.ParentNode = pNode;
        WillAdd.Rank = 0;
        WillAdd.NodeList = new List<long>();
        WillAdd.NodeList.Add(pNode);
        mNodeInfoDict[pNode] = WillAdd;
    }

    // 合併処理
    internal void Unite(long pX, long pY)
    {
        long XNode = FindSet(pX);
        long YNode = FindSet(pY);

        // 既に同じ木の場合
        if (XNode == YNode) return;

        long XRank = mNodeInfoDict[XNode].Rank;
        long YRank = mNodeInfoDict[YNode].Rank;

        RemoveVal(mNodeInfoDict[XNode].NodeList);
        RemoveVal(mNodeInfoDict[YNode].NodeList);

        if (XRank > YRank) {
            mNodeInfoDict[YNode].ParentNode = XNode;
            mNodeInfoDict[XNode].NodeList.AddRange(mNodeInfoDict[YNode].NodeList);
        }
        else {
            mNodeInfoDict[XNode].ParentNode = YNode;
            mNodeInfoDict[YNode].NodeList.AddRange(mNodeInfoDict[XNode].NodeList);
            if (XRank == YRank) {
                mNodeInfoDict[YNode].Rank++;
            }
        }
    }

    // Listの10件目以降をremove
    static void RemoveVal(List<long> pList)
    {
        foreach (long EachVal in pList.OrderByDescending(pX => pX).Skip(10)) {
            pList.Remove(EachVal);
        }
    }

    // ノードを引数として、木の根を取得
    internal long FindSet(long pTargetNode)
    {
        // 根までの経路上のノードのList
        var PathNodeList = new List<long>();

        long CurrNode = pTargetNode;
        while (CurrNode != mNodeInfoDict[CurrNode].ParentNode) {
            PathNodeList.Add(CurrNode);
            CurrNode = mNodeInfoDict[CurrNode].ParentNode;
        }

        // 経路圧縮 (親ポインタの付け替え)
        foreach (long EachPathNode in PathNodeList) {
            mNodeInfoDict[EachPathNode].ParentNode = CurrNode;
        }
        return CurrNode;
    }

    internal long GetAnswer(long pNode, long rank)
    {
        long RootNode = FindSet(pNode);
        var InsList = mNodeInfoDict[RootNode].NodeList;
        if (InsList.Count < rank) return -1;
        InsList = InsList.OrderByDescending(pX => pX).ToList();

        return InsList[(int)rank - 1];
    }

    internal void DebugPrint()
    {
        foreach (var EachPair in mNodeInfoDict.OrderBy(pX => pX.Key)) {
            Console.WriteLine("mNodeInfoDict[{0}].ParentNode={1}",
                EachPair.Key, EachPair.Value.ParentNode);
        }
    }
}


解説

UnionFindで木のノードのListを持つようにしてます。