典型問題    前の典型問題へ

典型問題(上級) C 各部分木の大きさ


問題へのリンク


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

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

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

        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);
        }

        ExecEulerTour();

        var IndListDict = new Dictionary<int, List<int>>();

        for (int I = 0; I <= mEulerTourJyoutaiList.Count - 1; I++) {
            int CurrNode = mEulerTourJyoutaiList[I].Node;
            if (IndListDict.ContainsKey(CurrNode) == false) {
                IndListDict[CurrNode] = new List<int>();
            }
            IndListDict[CurrNode].Add(I);
        }

        foreach (var EachPair in IndListDict.OrderBy(pX => pX.Key)) {
            long Range = EachPair.Value.Max() - EachPair.Value.Min() + 1;
            Console.WriteLine(Range / 2);
        }
    }

    struct JyoutaiDef
    {
        internal int Node;
        internal int Level;
        internal bool IsStart;
        internal bool IsEnd;
    }

    // オイラーツアーを行い、下記の2通りのタイミングでノードをAddする
    // 1 最初の訪問時
    // 2 子の部分木を訪問完了時
    static List<JyoutaiDef> mEulerTourJyoutaiList = new List<JyoutaiDef>();
    static void ExecEulerTour()
    {
        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.Node = 1;
        WillPush.Level = 0;

        WillPush.IsStart = false; WillPush.IsEnd = true;
        Stk.Push(WillPush);
        WillPush.IsStart = true; WillPush.IsEnd = false;
        Stk.Push(WillPush);

        var VisitedSet = new HashSet<int>();
        VisitedSet.Add(1);

        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();
            mEulerTourJyoutaiList.Add(Popped);
            if (Popped.IsEnd) {
                continue;
            }
            if (mToNodeListDict.ContainsKey(Popped.Node)) {
                foreach (int EachToNode in mToNodeListDict[Popped.Node]) {
                    if (VisitedSet.Add(EachToNode)) {
                        WillPush.Node = EachToNode;
                        WillPush.Level = Popped.Level + 1;

                        WillPush.IsStart = false; WillPush.IsEnd = true;
                        Stk.Push(WillPush);
                        WillPush.IsStart = true; WillPush.IsEnd = false;
                        Stk.Push(WillPush);
                    }
                }
            }
        }
    }
}


解説

オイラーツアーや木DPで解くことができます。