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

ABC049-D 連結


問題へのリンク


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

    static int mN;
    static int mK;
    static int mL;

    struct PQInfoDef
    {
        internal int P;
        internal int Q;
    }
    static List<PQInfoDef> mPQInfoList = new List<PQInfoDef>();

    struct RSInfoDef
    {
        internal int R;
        internal int S;
    }
    static List<RSInfoDef> mRSInfoList = new List<RSInfoDef>();

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

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

        SplitAct(InputList[0]);
        mN = wkArr[0];
        mK = wkArr[1];
        mL = wkArr[2];

        foreach (string EachStr in InputList.Skip(1).Take(mK)) {
            SplitAct(EachStr);
            PQInfoDef WillAdd;
            WillAdd.P = wkArr[0];
            WillAdd.Q = wkArr[1];
            mPQInfoList.Add(WillAdd);
        }

        foreach (string EachStr in InputList.Skip(1 + mK)) {
            SplitAct(EachStr);
            RSInfoDef WillAdd;
            WillAdd.R = wkArr[0];
            WillAdd.S = wkArr[1];
            mRSInfoList.Add(WillAdd);
        }
        Solve();
    }

    static void Solve()
    {
        var InsUnionFindPQ = new UnionFind();
        var InsUnionFindRS = new UnionFind();
        for (int I = 1; I <= mN; I++) {
            InsUnionFindPQ.MakeSet(I);
            InsUnionFindRS.MakeSet(I);
        }

        mPQInfoList.ForEach(pX => InsUnionFindPQ.Unite(pX.P, pX.Q));
        mRSInfoList.ForEach(pX => InsUnionFindRS.Unite(pX.R, pX.S));

        var HashCntDict = new Dictionary<string, int>();

        for (int I = 1; I <= mN; I++) {
            int RootPQ = InsUnionFindPQ.FindSet(I);
            int RootRS = InsUnionFindRS.FindSet(I);

            string Hash = string.Format("{0},{1}", RootPQ, RootRS);
            if (HashCntDict.ContainsKey(Hash) == false) {
                HashCntDict[Hash] = 0;
            }
            HashCntDict[Hash]++;
        }

        var AnswerList = new List<int>();
        for (int I = 1; I <= mN; I++) {
            int RootPQ = InsUnionFindPQ.FindSet(I);
            int RootRS = InsUnionFindRS.FindSet(I);

            string Hash = string.Format("{0},{1}", RootPQ, RootRS);
            AnswerList.Add(HashCntDict[Hash]);
        }

        string[] WillOutArr = Array.ConvertAll(AnswerList.ToArray(), pX => pX.ToString());
        Console.WriteLine(string.Join(" ", WillOutArr));
    }
}

// UnionFindクラス
internal class UnionFind
{
    private class NodeInfoDef
    {
        internal int ParentNode;
        internal int Rank;
    }
    private Dictionary<int, NodeInfoDef> mNodeInfoDict =
        new Dictionary<int, NodeInfoDef>();

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

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

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

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

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

        // 経路圧縮 (親ポインタの付け替え)
        foreach (int EachPathNode in PathNodeList) {
            NodeInfoDef wkNodeInfo = mNodeInfoDict[EachPathNode];
            wkNodeInfo.ParentNode = CurrNode;
            mNodeInfoDict[EachPathNode] = wkNodeInfo;
        }
        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);
        }
    }
}


解説

道路と鉄道のそれぞれでUnionFindクラスを用意して
Unite処理を行い、根ノードのペアをハッシュ値として、
件数を集計してます。