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

ABC146-D Coloring Edges on Tree


問題へのリンク


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

    // 隣接リスト
    static Dictionary<int, List<int>> mToNodeListDict = new Dictionary<int, List<int>>();

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

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

        int N = int.Parse(InputList[0]);

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

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

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

        // 枝の色番号[枝のHash値]なDict
        var EdgeColorDict = new Dictionary<long, int>();

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrNode = 1;
        WillPush.FromEdge = -1;
        Stk.Push(WillPush);

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

            int FromEdge = Popped.FromEdge;
            int NewFromEdge = 1;
            if (NewFromEdge == FromEdge) NewFromEdge++;

            foreach (int EachToNode in mToNodeListDict[Popped.CurrNode]) {
                if (VisitedSet.Add(EachToNode) == false) {
                    continue;
                }

                long CurrHash = DeriveHash(Popped.CurrNode, EachToNode);
                EdgeColorDict[CurrHash] = NewFromEdge;

                WillPush.CurrNode = EachToNode;
                WillPush.FromEdge = NewFromEdge;
                Stk.Push(WillPush);

                NewFromEdge++;
                if (NewFromEdge == FromEdge) NewFromEdge++;
            }
        }

        Console.WriteLine(EdgeColorDict.Values.Max());
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);
            int FromNode = wkArr[0];
            int ToNode = wkArr[1];
            long CurrHash = DeriveHash(FromNode, ToNode);
            Console.WriteLine(EdgeColorDict[CurrHash]);
        }
    }

    static long DeriveHash(long pFromNode, long pToNode)
    {
        return pFromNode * 1000000 + pToNode;
    }
}


解説

下記の深さ優先探索を行ってます。

どの枝から来たかを状態に持ち、
なるべく小さい、枝の色番号を振って、未訪問ノードに訪問する