結果
| 問題 | No.277 根掘り葉掘り |
| コンテスト | |
| ユーザー |
14番
|
| 提出日時 | 2016-05-12 02:32:00 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,987 bytes |
| 記録 | |
| コンパイル時間 | 937 ms |
| コンパイル使用メモリ | 115,192 KB |
| 実行使用メモリ | 56,432 KB |
| 最終ジャッジ日時 | 2024-10-05 13:53:23 |
| 合計ジャッジ時間 | 10,671 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 7 WA * 11 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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());
for(int i=0; i<pointCount - 1; i++) {
int[] inpt = Reader.GetInt();
TreeNode parent;
if(this.TreeDic.ContainsKey(inpt[0])) {
parent = this.TreeDic[inpt[0]];
} else
{
parent = new TreeNode(inpt[0]);
this.TreeDic.Add(inpt[0],parent);
}
TreeNode child;
if(this.TreeDic.ContainsKey(inpt[1])) {
child = this.TreeDic[inpt[1]];
} else
{
child = new TreeNode(inpt[1]);
this.TreeDic.Add(inpt[1], child);
}
parent.Add(child);
}
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)));
}
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 = @"
6
1 2
2 3
3 4
4 5
5 6
";
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();
}
}
14番