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

AOJ 0655 日本沈没


問題へのリンク


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

    struct NodeInfoDef
    {
        internal long Height;
        internal List<int> NearNodeList;
    }
    static Dictionary<int, NodeInfoDef> mNodeInfoDict = new Dictionary<int, NodeInfoDef>();

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();

        if (Array.TrueForAll(AArr, pX => pX == 0)) {
            Console.WriteLine(0);
            return;
        }

        int UB = AArr.GetUpperBound(0);
        for (int I = 0; I <= UB; I++) {
            if (AArr[I] == 0) continue;

            NodeInfoDef WillAdd;
            WillAdd.Height = AArr[I];
            WillAdd.NearNodeList = new List<int>();
            if (0 < I) WillAdd.NearNodeList.Add(I - 1);
            if (I < UB) WillAdd.NearNodeList.Add(I + 1);
            mNodeInfoDict[I] = WillAdd;
        }

        // 登場したノードのSet
        var AppearNodeSet = new HashSet<int>();

        // 高さごとの最終な木の数
        var ShimaCntDict = new Dictionary<long, long>();

        // 高さの降順に見ていく
        var InsUnionFindWithAnyInfo = new UnionFindWithAnyInfo();
        foreach (var EachPair in mNodeInfoDict.OrderByDescending(pX => pX.Value.Height)) {
            int CurrNode = EachPair.Key;
            AppearNodeSet.Add(CurrNode);
            long CurrHeight = EachPair.Value.Height;
            InsUnionFindWithAnyInfo.MakeSet(CurrNode);

            foreach (int EachNearNode in EachPair.Value.NearNodeList) {
                if (AppearNodeSet.Contains(EachNearNode) == false) continue;

                if (CurrHeight <= AArr[EachNearNode]) {
                    InsUnionFindWithAnyInfo.Unite(EachNearNode, CurrNode);
                }
            }
            ShimaCntDict[CurrHeight] = InsUnionFindWithAnyInfo.TreeCnt;
        }

        Console.WriteLine(ShimaCntDict.Values.Max());
    }
}

// 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して、
UnionFind森の中の、木の数を都度調べてます。