結果

問題 No.277 根掘り葉掘り
ユーザー 14番14番
提出日時 2016-05-13 00:43:58
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,095 ms / 3,000 ms
コード長 4,270 bytes
コンパイル時間 1,109 ms
コンパイル使用メモリ 112,292 KB
実行使用メモリ 104,000 KB
最終ジャッジ日時 2024-04-15 15:26:06
合計ジャッジ時間 12,040 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
25,396 KB
testcase_01 AC 37 ms
25,624 KB
testcase_02 AC 36 ms
25,652 KB
testcase_03 AC 38 ms
23,728 KB
testcase_04 AC 38 ms
25,488 KB
testcase_05 AC 38 ms
25,732 KB
testcase_06 AC 39 ms
27,776 KB
testcase_07 AC 39 ms
25,484 KB
testcase_08 AC 38 ms
25,648 KB
testcase_09 AC 1,089 ms
104,000 KB
testcase_10 AC 749 ms
55,692 KB
testcase_11 AC 806 ms
59,124 KB
testcase_12 AC 1,095 ms
92,668 KB
testcase_13 AC 863 ms
57,460 KB
testcase_14 AC 835 ms
57,716 KB
testcase_15 AC 848 ms
59,564 KB
testcase_16 AC 877 ms
60,156 KB
testcase_17 AC 810 ms
57,864 KB
testcase_18 AC 808 ms
57,836 KB
testcase_19 AC 819 ms
59,488 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];
        this.TreeDic = new TreeNode[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[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[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 != null && a.Items.Count == 0).ToList().ForEach(b=>task.Enqueue(b.ID));
        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.Where(c=>c!=null).OrderBy(a=>a.ID).ToList().ForEach(b=>Console.WriteLine(Math.Min(b.FarFromLeaf, b.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));
    }
    
    
    TreeNode[] TreeDic;
    
    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