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

ABC157-D Friend Suggestions


問題へのリンク


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 4 1");
            WillReturn.Add("2 1");
            WillReturn.Add("1 3");
            WillReturn.Add("3 2");
            WillReturn.Add("3 4");
            WillReturn.Add("4 1");
            //0 1 0 1
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("5 10 0");
            WillReturn.Add("1 2");
            WillReturn.Add("1 3");
            WillReturn.Add("1 4");
            WillReturn.Add("1 5");
            WillReturn.Add("3 2");
            WillReturn.Add("2 4");
            WillReturn.Add("2 5");
            WillReturn.Add("4 3");
            WillReturn.Add("5 3");
            WillReturn.Add("4 5");
            //0 0 0 0 0
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("10 9 3");
            WillReturn.Add("10 1");
            WillReturn.Add("6 7");
            WillReturn.Add("8 2");
            WillReturn.Add("2 5");
            WillReturn.Add("8 4");
            WillReturn.Add("7 3");
            WillReturn.Add("10 9");
            WillReturn.Add("6 4");
            WillReturn.Add("5 8");
            WillReturn.Add("2 6");
            WillReturn.Add("7 5");
            WillReturn.Add("3 1");
            //1 3 5 4 3 3 3 3 1 0
        }
        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(pX => int.Parse(pX)).ToArray();

        SplitAct(InputList[0]);
        int N = wkArr[0];
        int M = wkArr[1];
        int K = wkArr[2];

        // 隣接リストの作成
        var ToNodeListDict = new Dictionary<int, List<int>>();
        foreach (string EachStr in InputList.Skip(1).Take(M)) {
            SplitAct(EachStr);

            int FromNode = wkArr[0];
            int ToNode = wkArr[1];

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

        // 隣接リスト(ブロック情報)の作成
        var BlockListDict = new Dictionary<int, List<int>>();
        foreach (string EachStr in InputList.Skip(1 + M)) {
            SplitAct(EachStr);

            int FromNode = wkArr[0];
            int ToNode = wkArr[1];

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

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

        for (int I = 1; I <= N; I++) {
            if (ToNodeListDict.ContainsKey(I)) {
                foreach (int EachToNode in ToNodeListDict[I]) {
                    InsUnionFind.Unite(I, EachToNode);
                }
            }
        }

        // 木ごとの要素数を持つDict
        var CntDict = new Dictionary<int, int>();
        for (int I = 1; I <= N; I++) {
            int RootNode = InsUnionFind.FindSet(I);
            if (CntDict.ContainsKey(RootNode) == false) {
                CntDict[RootNode] = 1;
            }
            else {
                CntDict[RootNode]++;
            }
        }

        var AnswerList = new List<int>();
        for (int I = 1; I <= N; I++) {
            int RootNode = InsUnionFind.FindSet(I);
            int AnswerCnt = CntDict[RootNode];

            // 自分の分を引く
            AnswerCnt--;

            // 友達の分を引く
            if (ToNodeListDict.ContainsKey(I)) {
                AnswerCnt -= ToNodeListDict[I].Count;
            }

            // ブロックの分を引く
            if (BlockListDict.ContainsKey(I)) {
                foreach (int EachNode in BlockListDict[I]) {
                    int CurrRootNode = InsUnionFind.FindSet(EachNode);
                    if (RootNode == CurrRootNode) {
                        AnswerCnt--;
                    }
                }
            }
            AnswerList.Add(AnswerCnt);
        }

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

// 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クラスを使ってます。