AOJ本の読書メモ   AOJ    次のAOJの問題へ    前のAOJの問題へ

DSL_1_A: Disjoint Set: Union Find Tree


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

// Q043 UnionFind https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_A&lang=jp
class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("5 12");
            WillReturn.Add("0 1 4");
            WillReturn.Add("0 2 3");
            WillReturn.Add("1 1 2");
            WillReturn.Add("1 3 4");
            WillReturn.Add("1 1 4");
            WillReturn.Add("1 3 2");
            WillReturn.Add("0 1 3");
            WillReturn.Add("1 2 4");
            WillReturn.Add("1 3 0");
            WillReturn.Add("0 0 4");
            WillReturn.Add("1 0 2");
            WillReturn.Add("1 3 0");
            //0
            //0
            //1
            //1
            //1
            //0
            //1
            //1
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

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

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

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

        var InsUnionFind = new UnionFind();
        for (int I = 0; I <= N - 1; I++) {
            InsUnionFind.MakeSet(I);
        }

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            if (wkArr[0] == 0) { //Uniteクエリ
                InsUnionFind.Unite(wkArr[1], wkArr[2]);
            }
            if (wkArr[0] == 1) { //Sameクエリ
                long RootNode1 = InsUnionFind.FindSet(wkArr[1]);
                long RootNode2 = InsUnionFind.FindSet(wkArr[2]);
                Console.WriteLine(RootNode1 == RootNode2 ? 1 : 0);
            }
        }
    }
}

#region UnionFind
// UnionFindクラス
internal class UnionFind
{
    private class NodeInfoDef
    {
        internal long ParentNode;
        internal long Rank;
    }
    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;
        mNodeInfoDict[pNode] = WillAdd;
    }

    // 合併処理
    internal void Unite(long pX, long pY)
    {
        long XNode = FindSet(pX);
        long YNode = FindSet(pY);
        long XRank = mNodeInfoDict[XNode].Rank;
        long YRank = mNodeInfoDict[YNode].Rank;

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

    // ノードを引数として、木の根を取得
    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 void DebugPrint()
    {
        foreach (var EachPair in mNodeInfoDict.OrderBy(pX => pX.Key)) {
            Console.WriteLine("mNodeInfoDict[{0}].ParentNode={1}",
                EachPair.Key, EachPair.Value.ParentNode);
        }
    }
}
#endregion


解説

UnionFindクラスを使用してます。