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

ABC229-E Graph Destruction


問題へのリンク


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

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

    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]);
        int N = wkArr[0];

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            int FromNode = wkArr[0];
            int ToNode = wkArr[1];

            if (mToNodeListDict.ContainsKey(FromNode) == false) {
                mToNodeListDict[FromNode] = new List<int>();
            }
            if (mToNodeListDict.ContainsKey(ToNode) == false) {
                mToNodeListDict[ToNode] = new List<int>();
            }
            if (FromNode < ToNode) {
                mToNodeListDict[FromNode].Add(ToNode);
            }
            if (ToNode < FromNode) {
                mToNodeListDict[ToNode].Add(FromNode);
            }
        }

        var AnswerList = new List<long>();
        AnswerList.Add(0);
        var InsUnionFind = new UnionFindWithAnyInfo();
        for (int I = N; 1 <= I; I--) {
            InsUnionFind.MakeSet(I);
            if (mToNodeListDict.ContainsKey(I)) {
                foreach (int EachToNode in mToNodeListDict[I]) {
                    InsUnionFind.Unite(I, EachToNode);
                }
            }
            AnswerList.Add(InsUnionFind.TreeCnt);
        }
        var sb = new System.Text.StringBuilder();
        AnswerList.Reverse();

        foreach (int EachInt in AnswerList.Skip(1)) {
            sb.AppendLine(EachInt.ToString());
        }
        Console.Write(sb.ToString());
    }
}

// UnionFindWithAnyInfoクラス
internal class UnionFindWithAnyInfo
{
    private class NodeInfoDef
    {
        internal long ParentNode;
        internal long Rank;
        internal long Size;    // 木のノード数
        internal long EdgeCnt; // 木の枝の数
    }
    private Dictionary<long, NodeInfoDef> mNodeInfoDict =
        new Dictionary<long, NodeInfoDef>();

    // 森にある木の数
    private long mTreeCnt = 0;

    // 森にある木の数を返す
    internal long TreeCnt
    {
        get { return mTreeCnt; }
    }

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

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

        // 既に同じ木の場合
        if (XNode == YNode) {
            mNodeInfoDict[XNode].EdgeCnt++;
            return;
        }
        mTreeCnt--;

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

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

            mNodeInfoDict[XNode].EdgeCnt++;
            mNodeInfoDict[XNode].EdgeCnt += mNodeInfoDict[YNode].EdgeCnt;
        }
        else {
            mNodeInfoDict[XNode].ParentNode = YNode;
            mNodeInfoDict[YNode].Size += mNodeInfoDict[XNode].Size;

            mNodeInfoDict[YNode].EdgeCnt++;
            mNodeInfoDict[YNode].EdgeCnt += mNodeInfoDict[XNode].EdgeCnt;
            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 long GetSize(long pNode)
    {
        long RootNode = FindSet(pNode);
        return mNodeInfoDict[RootNode].Size;
    }

    // ノードを引数として、木の枝数を取得
    internal long GetEdgeCnt(long pNode)
    {
        long RootNode = FindSet(pNode);
        return mNodeInfoDict[RootNode].EdgeCnt;
    }

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


解説

UnionFindの、切断処理は、計算量が悪いので、
クエリを先読みして、逆から解を求めてます。