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

ABC420-E Reachability Query


問題へのリンク


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("5 12");
            WillReturn.Add("3 2");
            WillReturn.Add("2 2");
            WillReturn.Add("3 2");
            WillReturn.Add("1 2 5");
            WillReturn.Add("1 3 4");
            WillReturn.Add("3 4");
            WillReturn.Add("3 5");
            WillReturn.Add("1 4 5");
            WillReturn.Add("1 1 3");
            WillReturn.Add("3 1");
            WillReturn.Add("2 2");
            WillReturn.Add("3 1");
            //No
            //Yes
            //No
            //Yes
            //Yes
            //No
        }
        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 void Main()
    {
        List<string> InputList = GetInputList();

        long[] wkArr = { };
        Action<string> SplitAct = (pStr) => wkArr = GetSplitArr(pStr);

        SplitAct(InputList[0]);
        long NodeCnt = wkArr[0];

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

        var BlackNodeSet = new HashSet<long>();

        var sb = new System.Text.StringBuilder();
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);

            long Type = wkArr[0];
            if (Type == 1) {
                long U = wkArr[1];
                long V = wkArr[2];
                InsUnionFind.Unite(U, V);
            }
            if (Type == 2) {
                long U = wkArr[1];
                if (BlackNodeSet.Contains(U)) {
                    BlackNodeSet.Remove(U);
                    InsUnionFind.BlackNodeRemove(U);
                }
                else {
                    BlackNodeSet.Add(U);
                    InsUnionFind.BlackNodeAdd(U);
                }
            }
            if (Type == 3) {
                long U = wkArr[1];
                long BlackNodeCnt = InsUnionFind.GetBlackNodeCnt(U);
                if (BlackNodeCnt == 0) {
                    Console.WriteLine("No");
                }
                else {
                    Console.WriteLine("Yes");
                }

            }
        }
        Console.Write(sb.ToString());
    }
}

// UnionFindWithAnyInfoクラス
internal class UnionFindWithAnyInfo
{
    private class NodeInfoDef
    {
        internal long ParentNode;
        internal long Rank;
        internal long Size;    // 木のノード数
        internal long EdgeCnt; // 木の枝の数
        internal HashSet<long> BlackNodeSet;
    }
    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;
        WillAdd.BlackNodeSet = new HashSet<long>();
        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;

            // マージテク
            mNodeInfoDict[XNode].BlackNodeSet = MergeTechnic_UnionWith<long>.ExecUnionWith(
                ref mNodeInfoDict[XNode].BlackNodeSet,
                ref mNodeInfoDict[YNode].BlackNodeSet);
        }
        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++;
            }

            // マージテク
            mNodeInfoDict[YNode].BlackNodeSet = MergeTechnic_UnionWith<long>.ExecUnionWith(
                ref mNodeInfoDict[XNode].BlackNodeSet,
                ref mNodeInfoDict[YNode].BlackNodeSet);
        }
    }

    // ノードを引数として、木の根を取得
    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 long GetBlackNodeCnt(long pNode)
    {
        long RootNode = FindSet(pNode);
        return mNodeInfoDict[RootNode].BlackNodeSet.Count;
    }

    // ノードを引数として、木に黒ノードをAdd
    internal void BlackNodeAdd(long pNode)
    {
        long RootNode = FindSet(pNode);
        mNodeInfoDict[RootNode].BlackNodeSet.Add(pNode);
    }

    // ノードを引数として、木の黒ノードをRemove
    internal void BlackNodeRemove(long pNode)
    {
        long RootNode = FindSet(pNode);
        mNodeInfoDict[RootNode].BlackNodeSet.Remove(pNode);
    }

    internal void DebugPrint()
    {
        foreach (var EachPair in mNodeInfoDict.OrderBy(pX => pX.Key)) {
            Console.WriteLine("mNodeInfoDict[{0}].ParentNode={1}",
                EachPair.Key, EachPair.Value.ParentNode);
        }
    }
}
#region MergeTechnic_UnionWith
// マージテクでHashSetのUnionWith
internal class MergeTechnic_UnionWith<Type>
{
    // HashSet2つを引数として、UnionWithしたHashSetを返す
    // 引数にした2つのHashSetはクリアする
    static internal HashSet<Type> ExecUnionWith(ref HashSet<Type> pSet1, ref HashSet<Type> pSet2)
    {
        // HashSetのUnionWithは、
        // 要素数の少ない集合から
        // 要素数の多い集合にコピーするほうが
        // 高速なので、要素数を比較
        HashSet<Type> SmallHashSet;
        HashSet<Type> LargeHashSet;
        if (pSet1.Count > pSet2.Count) {
            SmallHashSet = pSet2; LargeHashSet = pSet1;
        }
        else {
            SmallHashSet = pSet1; LargeHashSet = pSet2;
        }
        LargeHashSet.UnionWith(SmallHashSet);

        // 引数の2つのHashSetはクリア
        pSet1 = new HashSet<Type>();
        pSet2 = new HashSet<Type>();

        return LargeHashSet;
    }
}
#endregion


解説

UnionFindの代表ノードに、
黒ノードのHashSetを持たせてます。