結果

問題 No.277 根掘り葉掘り
ユーザー 14番14番
提出日時 2016-05-13 00:38:46
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,359 ms / 3,000 ms
コード長 4,261 bytes
コンパイル時間 1,037 ms
コンパイル使用メモリ 113,896 KB
実行使用メモリ 108,932 KB
最終ジャッジ日時 2024-04-15 15:25:35
合計ジャッジ時間 13,109 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
19,584 KB
testcase_01 AC 39 ms
19,584 KB
testcase_02 AC 40 ms
19,712 KB
testcase_03 AC 40 ms
19,712 KB
testcase_04 AC 40 ms
19,456 KB
testcase_05 AC 40 ms
19,456 KB
testcase_06 AC 41 ms
19,712 KB
testcase_07 AC 41 ms
19,584 KB
testcase_08 AC 39 ms
19,328 KB
testcase_09 AC 1,359 ms
108,932 KB
testcase_10 AC 813 ms
54,916 KB
testcase_11 AC 826 ms
59,340 KB
testcase_12 AC 1,026 ms
80,860 KB
testcase_13 AC 947 ms
59,788 KB
testcase_14 AC 874 ms
56,088 KB
testcase_15 AC 892 ms
56,328 KB
testcase_16 AC 862 ms
56,476 KB
testcase_17 AC 855 ms
56,824 KB
testcase_18 AC 930 ms
56,196 KB
testcase_19 AC 890 ms
56,088 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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());
        

        List<int>[] lineDic = new List<int>[pointCount + 1];
        for(int i=0; i<pointCount - 1; i++) {
            int[] inpt = Reader.GetInt();
            if(lineDic[inpt[0]] == null) {
                lineDic[inpt[0]] = new List<int>();
                this.TreeDic.Add(inpt[0], new TreeNode(inpt[0]));
            }
            lineDic[inpt[0]].Add(inpt[1]);
            if(lineDic[inpt[1]] == null) {
                lineDic[inpt[1]] = new List<int>();
                this.TreeDic.Add(inpt[1], new TreeNode(inpt[1]));
            }
            lineDic[inpt[1]].Add(inpt[0]);
        }

        this.SetTree(1, lineDic);

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

    }
    
    private void SetTree(int parent, List<int>[] dic) {
        dic[parent].ForEach((a)=>{
           if(TreeDic[parent].Parent == null || a != TreeDic[parent].Parent.ID) {
               TreeDic[parent].Add(TreeDic[a]);
           } 
        });
        TreeDic[parent].Items.ForEach(a=>this.SetTree(a.ID, dic));
    }
    
    
    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 = @"








 
";
        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