結果

問題 No.806 木を道に
ユーザー to_to_0121to_to_0121
提出日時 2019-03-22 22:10:55
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,382 bytes
コンパイル時間 2,913 ms
コンパイル使用メモリ 109,132 KB
実行使用メモリ 46,572 KB
最終ジャッジ日時 2023-10-19 09:31:19
合計ジャッジ時間 8,674 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
24,172 KB
testcase_01 AC 28 ms
24,172 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 27 ms
24,164 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 28 ms
24,164 KB
testcase_08 AC 29 ms
24,164 KB
testcase_09 AC 30 ms
24,164 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 109 ms
36,152 KB
testcase_25 AC 151 ms
40,352 KB
testcase_26 AC 153 ms
33,092 KB
testcase_27 AC 1,013 ms
46,572 KB
testcase_28 AC 32 ms
24,176 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;
class Program
    {
        public class Node
        {
            public int id;
            public List<Node> link = new List<Node>();

            public Node(int i)
            {
                id = i;
                link = new List<Node>();
            }
        }

        static void connect (Node a, Node b)
        {
            a.link.Add(b);
            b.link.Add(a);
            return;
        }
    	static void Main(string[] args)
    	{
            int n = int.Parse(Console.ReadLine());
            List<Node> nodes = new List<Node>();
            for (int i=0; i<n; i++) nodes.Add(new Node(i));
            for (int i=0; i<n-1; i++)
            {
                string[] s = Console.ReadLine().Split(' ');
                connect(nodes[int.Parse(s[0]) - 1], nodes[int.Parse(s[1]) - 1]);
            }
            int[] searchList = new int[n];
            for(int i=0; i<n; i++) searchList[i] = -1;
            List<int> queue = new List<int>();
            queue.Add(0);
            searchList[0] = 0;
            int lastId = 0;
            while(queue.Count > 0)
            {
                int id = queue[0];
                foreach(Node node in nodes[id].link)
                {
                    if (searchList[node.id] < 0){
                        searchList[node.id] = searchList[id] + 1;
                        queue.Add(node.id);
                    }
                }
                lastId = id;
                queue.Remove(id);
            }
            //Console.Error.WriteLine(lastId);
            int start = lastId;

            queue.Add(lastId);
            for(int i=0; i<n; i++) searchList[i] = -1;
            searchList[lastId] = 0;
            while(queue.Count > 0)
            {
                int id = queue[0];
                foreach(Node node in nodes[id].link)
                {
                    if (searchList[node.id] < 0){
                        searchList[node.id] = searchList[id] + 1;
                        queue.Add(node.id);
                    }
                }
                lastId = id;
                queue.Remove(id);
            }
            //Console.Error.WriteLine(start +  " " + lastId + " " + searchList[lastId]);
            Console.WriteLine(n - 1 - searchList[lastId]);
            
        }
    }
    //mcs Main.cs
    //mono Main.exe
0