結果

問題 No.277 根掘り葉掘り
ユーザー sekiya9311sekiya9311
提出日時 2017-10-06 00:33:31
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 357 ms / 3,000 ms
コード長 3,449 bytes
コンパイル時間 2,609 ms
コンパイル使用メモリ 109,796 KB
実行使用メモリ 50,000 KB
最終ジャッジ日時 2023-08-10 14:55:08
合計ジャッジ時間 8,856 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
23,736 KB
testcase_01 AC 60 ms
21,936 KB
testcase_02 AC 65 ms
23,732 KB
testcase_03 AC 64 ms
19,772 KB
testcase_04 AC 66 ms
23,776 KB
testcase_05 AC 66 ms
23,668 KB
testcase_06 AC 64 ms
23,876 KB
testcase_07 AC 66 ms
21,752 KB
testcase_08 AC 63 ms
21,776 KB
testcase_09 AC 240 ms
50,000 KB
testcase_10 AC 213 ms
42,956 KB
testcase_11 AC 261 ms
42,528 KB
testcase_12 AC 357 ms
45,848 KB
testcase_13 AC 322 ms
38,564 KB
testcase_14 AC 293 ms
43,044 KB
testcase_15 AC 313 ms
39,460 KB
testcase_16 AC 310 ms
40,804 KB
testcase_17 AC 306 ms
40,872 KB
testcase_18 AC 310 ms
40,668 KB
testcase_19 AC 313 ms
36,532 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.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProgrammingContest
{
    class MainClass
    {
        Scanner sc;
        static void Main(string[] args)
        {
            new MainClass().Solve();
        }

        struct Point
        {
            public double x, y;
        }

        const bool DEBUG = false;   // TODO: must check!!
        void Solve()
        {
            if (DEBUG)
            {
                string backPath = "..";
                char dirSep = System.IO.Path.DirectorySeparatorChar;
                string inFilePath = backPath + dirSep + backPath + dirSep + "in.txt";
                sc = new Scanner(new System.IO.StreamReader(inFilePath));
            }
            else
            {
                sc = new Scanner();
            }
            int N = sc.NextInt;
            G = new List<int>[N].Select(lst => new List<int>()).ToArray();
            ans = new int[N].Select(el => INF).ToArray();
            for (int i = 0; i < N - 1; i++)
            {
                int a = sc.NextInt - 1;
                int b = sc.NextInt - 1;
                G[a].Add(b);
                G[b].Add(a);
            }
            dfs(0, -1, 0);
            for (int i = 1; i < N; i++)
            {
                if (G[i].Count <= 1)
                {
                    dfs(i, -1, 0);
                }
            }
            Console.WriteLine(String.Join("\n", Array.ConvertAll(ans, el => el.ToString())));
        }
        void dfs(int now, int prev, int cost)
        {
            ans[now] = Math.Min(ans[now], cost);
            foreach (int el in G[now])
            {
                if (el != prev)
                {
                    if (cost + 1 < ans[el])
                    {
                        dfs(el, now, cost + 1);
                    }
                }
            }
        }
        List<int>[] G;
        const int INF = (int)1e9;
        int[] ans;
    }
    class Scanner
    {
        Queue<String> buffer;
        char[] sep;
        System.IO.TextReader reader;
        public Scanner(System.IO.TextReader reader = null)
        {
            this.buffer = new Queue<string>();
            this.sep = new char[] { ' ' };
            this.reader = (reader ?? Console.In);
        }
        private void CheckBuffer()
        {
            if (this.buffer.Count == 0)
            {
                String[] sreArray = this.reader.ReadLine().Split(this.sep);
                foreach (String elStr in sreArray)
                {
                    this.buffer.Enqueue(elStr);
                }
            }
        }

        public String Next
        {
            get
            {
                this.CheckBuffer();
                return this.buffer.Dequeue();
            }
        }

        public int NextInt
        {
            get
            {
                return int.Parse(this.Next);
            }
        }
        public double NextDouble
        {
            get
            {
                return double.Parse(this.Next);
            }
        }

        public long NextLong
        {
            get
            {
                return long.Parse(this.Next);
            }
        }
        public bool IsEmpty
        {
            get
            {
                this.CheckBuffer();
                return this.buffer.Count == 0;
            }
        }
    }
}
0