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

ALDS1_7_A: Rooted Trees


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

// Q023 根付き木の表現 https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_7_A&lang=jp
class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("13");
            WillReturn.Add("0 3 1 4 10");
            WillReturn.Add("1 2 2 3");
            WillReturn.Add("2 0");
            WillReturn.Add("3 0");
            WillReturn.Add("4 3 5 6 7");
            WillReturn.Add("5 0");
            WillReturn.Add("6 0");
            WillReturn.Add("7 2 8 9");
            WillReturn.Add("8 0");
            WillReturn.Add("9 0");
            WillReturn.Add("10 2 11 12");
            WillReturn.Add("11 0");
            WillReturn.Add("12 0");
            //node 0: parent = -1, depth = 0, root, [1, 4, 10]
            //node 1: parent = 0, depth = 1, internal node, [2, 3]
            //node 2: parent = 1, depth = 2, leaf, []
            //node 3: parent = 1, depth = 2, leaf, []
            //node 4: parent = 0, depth = 1, internal node, [5, 6, 7]
            //node 5: parent = 4, depth = 2, leaf, []
            //node 6: parent = 4, depth = 2, leaf, []
            //node 7: parent = 4, depth = 2, internal node, [8, 9]
            //node 8: parent = 7, depth = 3, leaf, []
            //node 9: parent = 7, depth = 3, leaf, []
            //node 10: parent = 0, depth = 1, internal node, [11, 12]
            //node 11: parent = 10, depth = 2, leaf, []
            //node 12: parent = 10, depth = 2, leaf, []
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4");
            WillReturn.Add("1 3 3 2 0");
            WillReturn.Add("0 0");
            WillReturn.Add("3 0");
            WillReturn.Add("2 0");
            //node 0: parent = 1, depth = 1, leaf, []
            //node 1: parent = -1, depth = 0, root, [3, 2, 0]
            //node 2: parent = 1, depth = 1, leaf, []
            //node 3: parent = 1, depth = 1, leaf, []
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static Dictionary<int, int[]> mNodeInfoDict = new Dictionary<int, int[]>();

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

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

        // 処理01 入力をDictに保存
        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);

            int NodeID = wkArr[0];
            int[] ChildNodeArr = wkArr.Skip(2).ToArray();
            mNodeInfoDict[NodeID] = ChildNodeArr;
        }

        // 処理02 木の根をO(n)で求める
        var ChildNodeSet = new HashSet<int>();
        foreach (var EachPair in mNodeInfoDict) {
            ChildNodeSet.UnionWith(EachPair.Value);
        }
        int RootNode = -1;
        foreach (int EachKey in mNodeInfoDict.Keys) {
            if (ChildNodeSet.Contains(EachKey) == false) {
                RootNode = EachKey;
            }
        }

        // 処理03 根からDFSで各ノードの情報を求める
        List<JyoutaiDef> JyoutaiList = ExecDFS(RootNode);
        JyoutaiList.Sort((A, B) => A.NodeID.CompareTo(B.NodeID));

        // 処理04 各ノードの情報を出力する
        foreach (JyoutaiDef EachJyoutai in JyoutaiList) {
            string NodeInfo = "";
            int NodeID = EachJyoutai.NodeID;
            if (NodeID == RootNode) {
                NodeInfo = "root";
            }
            else if (mNodeInfoDict[NodeID].Length == 0) {
                NodeInfo = "leaf";
            }
            else {
                NodeInfo = "internal node";
            }
            Console.WriteLine("node {0}: parent = {1}, depth = {2}, {3}, [{4}]",
                NodeID, EachJyoutai.ParentNode, EachJyoutai.Depth, NodeInfo,
                string.Join(", ", Array.ConvertAll(mNodeInfoDict[NodeID], pX => pX.ToString())));
        }
    }

    struct JyoutaiDef
    {
        internal int NodeID;
        internal int ParentNode;
        internal int Depth;
    }

    // 処理03 根からDFSで各ノードの情報を求める
    static List<JyoutaiDef> ExecDFS(int pRootNode)
    {
        var WillReturn = new List<JyoutaiDef>();

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.NodeID = pRootNode;
        WillPush.ParentNode = -1;
        WillPush.Depth = 0;

        Stk.Push(WillPush);
        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();
            WillReturn.Add(Popped);

            foreach (int EachChild in mNodeInfoDict[Popped.NodeID]) {
                WillPush.NodeID = EachChild;
                WillPush.ParentNode = Popped.NodeID;
                WillPush.Depth = Popped.Depth + 1;
                Stk.Push(WillPush);
            }
        }
        return WillReturn;
    }
}


解説

根から深さ優先探索してます。