結果

問題 No.277 根掘り葉掘り
ユーザー 14番14番
提出日時 2016-05-12 22:19:16
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,225 ms / 3,000 ms
コード長 4,267 bytes
コンパイル時間 1,180 ms
コンパイル使用メモリ 114,892 KB
実行使用メモリ 103,816 KB
最終ジャッジ日時 2024-04-15 15:24:17
合計ジャッジ時間 13,826 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
19,712 KB
testcase_01 AC 43 ms
19,712 KB
testcase_02 AC 44 ms
19,456 KB
testcase_03 AC 43 ms
19,840 KB
testcase_04 AC 44 ms
19,712 KB
testcase_05 AC 44 ms
19,584 KB
testcase_06 AC 44 ms
19,584 KB
testcase_07 AC 44 ms
19,840 KB
testcase_08 AC 42 ms
19,584 KB
testcase_09 AC 1,225 ms
103,816 KB
testcase_10 AC 821 ms
60,516 KB
testcase_11 AC 869 ms
59,084 KB
testcase_12 AC 1,169 ms
89,312 KB
testcase_13 AC 1,059 ms
59,416 KB
testcase_14 AC 917 ms
59,576 KB
testcase_15 AC 946 ms
61,608 KB
testcase_16 AC 899 ms
60,460 KB
testcase_17 AC 895 ms
60,220 KB
testcase_18 AC 891 ms
59,780 KB
testcase_19 AC 906 ms
59,804 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