結果

問題 No.277 根掘り葉掘り
ユーザー 14番14番
提出日時 2016-05-12 22:19:16
言語 C#
(csc 3.9.0)
結果
AC  
実行時間 968 ms / 3,000 ms
コード長 4,267 bytes
コンパイル時間 2,808 ms
使用メモリ 100,264 KB
最終ジャッジ日時 2022-11-28 15:41:17
合計ジャッジ時間 13,750 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 65 ms
15,868 KB
testcase_01 AC 66 ms
15,832 KB
testcase_02 AC 67 ms
15,832 KB
testcase_03 AC 66 ms
15,776 KB
testcase_04 AC 64 ms
15,932 KB
testcase_05 AC 65 ms
15,916 KB
testcase_06 AC 66 ms
15,940 KB
testcase_07 AC 68 ms
15,860 KB
testcase_08 AC 64 ms
15,776 KB
testcase_09 AC 968 ms
100,264 KB
testcase_10 AC 656 ms
56,872 KB
testcase_11 AC 684 ms
55,332 KB
testcase_12 AC 914 ms
85,488 KB
testcase_13 AC 765 ms
55,896 KB
testcase_14 AC 687 ms
56,104 KB
testcase_15 AC 744 ms
58,212 KB
testcase_16 AC 722 ms
56,844 KB
testcase_17 AC 695 ms
56,600 KB
testcase_18 AC 710 ms
56,208 KB
testcase_19 AC 725 ms
55,948 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());
        
        Dictionary<int, List<int>> lineDic = new Dictionary<int, List<int>>();
        
        for(int i=0; i<pointCount - 1; i++) {
            int[] inpt = Reader.GetInt();
            if(!lineDic.ContainsKey(inpt[0])) {
                lineDic.Add(inpt[0], new List<int>());
            }
            lineDic[inpt[0]].Add(inpt[1]);
            if(!lineDic.ContainsKey(inpt[1])) {
                lineDic.Add(inpt[1], new List<int>());
            }
            lineDic[inpt[1]].Add(inpt[0]);
        }
        lineDic.ToList().ForEach(a=>this.TreeDic.Add(a.Key, new TreeNode(a.Key)));
        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, Dictionary<int, 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