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

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

    struct EdgeInfoDef
    {
        internal long FromNode;
        internal long ToNode;
        internal long Cost;
    }
    static List<EdgeInfoDef> mEdgeInfoList = new List<EdgeInfoDef>();

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

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

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

        long Answer = 0;
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);

            EdgeInfoDef WillAdd;
            WillAdd.FromNode = wkArr[0];
            WillAdd.ToNode = wkArr[1];
            WillAdd.Cost = wkArr[2];
            mEdgeInfoList.Add(WillAdd);

            if (WillAdd.Cost > 0) {
                Answer += WillAdd.Cost;
            }
        }

        // クラスカル法で解く
        var InsUnionFind = new UnionFind();
        for (long I = 1; I <= N; I++) {
            InsUnionFind.MakeSet(I);
        }

        // 枝をコストの昇順にソート
        mEdgeInfoList.Sort((a, b) => a.Cost.CompareTo(b.Cost));

        foreach (EdgeInfoDef EachEdgeInfo in mEdgeInfoList) {
            long FromNode = EachEdgeInfo.FromNode;
            long ToNode = EachEdgeInfo.ToNode;
            long RootNode1 = InsUnionFind.FindSet(EachEdgeInfo.FromNode);
            long RootNode2 = InsUnionFind.FindSet(EachEdgeInfo.ToNode);
            if (RootNode1 != RootNode2) {
                InsUnionFind.Unite(FromNode, ToNode);

                if (EachEdgeInfo.Cost > 0) {
                    Answer -= EachEdgeInfo.Cost;
                }
            }
        }
        Console.WriteLine(Answer);
    }
}

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

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

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

        if (XRank > YRank) {
            mNodeInfoDict[YNode].ParentNode = XNode;
        }
        else {
            mNodeInfoDict[XNode].ParentNode = YNode;
            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) {
            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);
        }
    }
}


解説

グラフの正の辺の総合計を、仮の解にしておいて、
MSTの構築で、正の辺を使ったら、仮の解から引いてます。