結果

問題 No.277 根掘り葉掘り
ユーザー 14番14番
提出日時 2016-05-12 02:32:00
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 3,987 bytes
コンパイル時間 937 ms
コンパイル使用メモリ 115,192 KB
実行使用メモリ 56,432 KB
最終ジャッジ日時 2024-10-05 13:53:23
合計ジャッジ時間 10,671 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
25,652 KB
testcase_01 AC 38 ms
23,472 KB
testcase_02 AC 38 ms
25,336 KB
testcase_03 AC 39 ms
25,464 KB
testcase_04 AC 39 ms
27,656 KB
testcase_05 AC 39 ms
25,524 KB
testcase_06 AC 39 ms
23,608 KB
testcase_07 WA -
testcase_08 AC 38 ms
25,220 KB
testcase_09 WA -
testcase_10 AC 655 ms
54,204 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        int pointCount = int.Parse(Reader.ReadLine());
        for(int i=0; i<pointCount - 1; i++) {
            int[] inpt = Reader.GetInt();
            TreeNode parent;
            if(this.TreeDic.ContainsKey(inpt[0])) {
                parent = this.TreeDic[inpt[0]];
            } else
            {
                parent = new TreeNode(inpt[0]);
                this.TreeDic.Add(inpt[0],parent);
            }
            TreeNode child;
            if(this.TreeDic.ContainsKey(inpt[1])) {
                child = this.TreeDic[inpt[1]];
            } else
            {
                child = new TreeNode(inpt[1]);
                this.TreeDic.Add(inpt[1], child);
            }
            parent.Add(child);
        }
        Queue<int> task = new Queue<int>();
        this.TreeDic.Where(a=>a.Value.Items.Count == 0).ToList().ForEach(b=>task.Enqueue(b.Key));
        while (task.Count > 0)
        {
            int key = task.Dequeue();
            TreeNode target = TreeDic[key];
            if(target.Parent != null && (target.Parent.FarFromLeaf == 0 || target.Parent.FarFromLeaf > target.FarFromLeaf + 1)) {
                target.Parent.FarFromLeaf = target.FarFromLeaf + 1;
                task.Enqueue(target.Parent.ID);
            }
            target.Items.ForEach((a)=>{
                if((a.FarFromLeaf == 0 && a.Items.Count > 0) || a.FarFromLeaf > target.FarFromLeaf + 1) {
                    a.FarFromLeaf = target.FarFromLeaf + 1;
                    task.Enqueue(a.ID);
                }
            });
        }
        this.TreeDic.OrderBy(a=>a.Key).ToList().ForEach(b=>Console.WriteLine(Math.Min(b.Value.FarFromLeaf, b.Value.FarFromRoot)));

    }
    
    Dictionary<int, TreeNode> TreeDic = new Dictionary<int, TreeNode>();
    
    public class TreeNode {
        public int ID;
        public TreeNode Parent;
        public List<TreeNode> Items = new List<TreeNode>();
        
        public int FarFromRoot;
        public int FarFromLeaf;
        
        public void Add(TreeNode child) {
            this.Items.Add(child);
            child.Parent = this;
            child.FarFromRoot = this.FarFromRoot + 1;
            child.RefreshFarFromRoot();
        }
        
        private void RefreshFarFromRoot() {
            if(this.Parent == null) {
                this.FarFromRoot = 0;
            } else
            {
                this.FarFromRoot = this.Parent.FarFromRoot + 1;
            }
            if(this.Items.Count > 0) {
                this.Items.ForEach(a=>a.RefreshFarFromRoot());
            }
        }
        
        public TreeNode(int id) {
            this.ID = id;
        }
    }


    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"




6
1 2
2 3
3 4
4 5
5 6





 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
        public static int[] GetInt(char delimiter = ' ', bool trim = false)
        {
            string inptStr = ReadLine();
            if (trim)
            {
                inptStr = inptStr.Trim();
            }
            string[] inpt = inptStr.Split(delimiter);
            int[] ret = new int[inpt.Length];
            for (int i = 0; i < inpt.Length; i++)
            {
                ret[i] = int.Parse(inpt[i]);
            }
            return ret;
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0