結果

問題 No.277 根掘り葉掘り
ユーザー 14番14番
提出日時 2016-05-13 00:42:08
言語 C#(csc)
(csc 3.9.0)
結果
RE  
実行時間 -
コード長 4,239 bytes
コンパイル時間 1,060 ms
コンパイル使用メモリ 115,608 KB
実行使用メモリ 96,400 KB
最終ジャッジ日時 2024-04-15 15:25:41
合計ジャッジ時間 6,589 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.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.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