AtCoderのARC    次のARCの問題へ    前のARCの問題へ

ARC108-C Keep Graph Connected


問題へのリンク


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

    static int mN;

    struct EdgeInfoDef
    {
        internal int ToNode;
        internal int Label;
    }
    static Dictionary<int, List<EdgeInfoDef>> mEdgeInfoListDict = new Dictionary<int, List<EdgeInfoDef>>();

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

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

            Action<int, int, int> AddEdgeAct = (pFromNode, pToNode, pLabel) =>
            {
                if (mEdgeInfoListDict.ContainsKey(pFromNode) == false) {
                    mEdgeInfoListDict[pFromNode] = new List<EdgeInfoDef>();
                }
                EdgeInfoDef WillAdd;
                WillAdd.ToNode = pToNode;
                WillAdd.Label = pLabel;
                mEdgeInfoListDict[pFromNode].Add(WillAdd);
            };
            AddEdgeAct(FromNode, ToNode, Label);
            AddEdgeAct(ToNode, FromNode, Label);
        }

        // 処理01 全ての枝のコストを1として、クラスカル法で全域木を求める

        // 全域木の枝のSet
        var SpanningTreeEdgeSet = new HashSet<long>();

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

        foreach (var EachPair in mEdgeInfoListDict) {
            foreach (EdgeInfoDef EachEdge in EachPair.Value) {
                int FromNode = EachPair.Key;
                int ToNode = EachEdge.ToNode;

                int RootNode1 = InsUnionFind.FindSet(FromNode);
                int RootNode2 = InsUnionFind.FindSet(ToNode);
                if (RootNode1 != RootNode2) {
                    InsUnionFind.Unite(FromNode, ToNode);
                    SpanningTreeEdgeSet.Add(GetHash(FromNode, ToNode));
                }
            }
        }

        // 処理02 ノード1を根として、DFSを行う
        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrNode = 1;
        WillPush.CurrVal = 1;
        Stk.Push(WillPush);

        var VisitedSet = new HashSet<int>();
        VisitedSet.Add(1);
        var JyoutaiList = new List<JyoutaiDef>();
        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();
            JyoutaiList.Add(Popped);

            if (mEdgeInfoListDict.ContainsKey(Popped.CurrNode)) {
                foreach (EdgeInfoDef EachEdge in mEdgeInfoListDict[Popped.CurrNode]) {
                    long EdgeHash = GetHash(Popped.CurrNode, EachEdge.ToNode);
                    if (SpanningTreeEdgeSet.Contains(EdgeHash) == false) continue;
                    if (VisitedSet.Add(EachEdge.ToNode) == false) continue;

                    WillPush.CurrNode = EachEdge.ToNode;
                    int CurrEdgeLabel = EachEdge.Label;
                    if (Popped.CurrVal == CurrEdgeLabel) {
                        for (int I = 1; I <= mN; I++) {
                            if (I != CurrEdgeLabel) {
                                WillPush.CurrVal = I;
                                break;
                            }
                        }
                    }
                    else {
                        WillPush.CurrVal = CurrEdgeLabel;
                    }
                    Stk.Push(WillPush);
                }
            }
        }

        var sb = new System.Text.StringBuilder();
        foreach (JyoutaiDef EachJyoutai in JyoutaiList.OrderBy(pX => pX.CurrNode)) {
            sb.Append(EachJyoutai.CurrVal);
            sb.AppendLine();
        }
        Console.Write(sb.ToString());
    }

    struct JyoutaiDef
    {
        internal int CurrNode;
        internal int CurrVal;
    }

    // 無向辺のハッシュ値を求める
    static long GetHash(int pFromNode, int pToNode)
    {
        long MinNode = Math.Min(pFromNode, pToNode);
        long MaxNode = Math.Max(pFromNode, pToNode);
        return MinNode * 1000000000 + MaxNode;
    }
}

#region UnionFind
// 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) {
            mNodeInfoDict[EachPathNode].ParentNode = CurrNode;
        }
        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);
        }
    }
}
#endregion


解説

根付き木を作ることができれば、解を求めることができるので、
クラスカル法で全域木を作ってから、木の根からDFSしてます。