結果

問題 No.806 木を道に
ユーザー ttrttr
提出日時 2020-02-08 21:17:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 656 bytes
コンパイル時間 201 ms
コンパイル使用メモリ 12,032 KB
実行使用メモリ 33,920 KB
最終ジャッジ日時 2024-04-08 11:28:15
合計ジャッジ時間 9,582 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,112 KB
testcase_01 AC 30 ms
10,112 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 32 ms
10,112 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 30 ms
10,112 KB
testcase_08 AC 29 ms
10,112 KB
testcase_09 AC 30 ms
10,112 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 248 ms
23,040 KB
testcase_25 AC 363 ms
29,696 KB
testcase_26 AC 150 ms
16,000 KB
testcase_27 AC 419 ms
29,312 KB
testcase_28 AC 40 ms
10,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
E = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = map(int, input().split())
    E[a-1].append(b-1)
    E[b-1].append(a-1)

from collections import deque

def f(s):
    q = deque([s])
    dist = [-1]*N
    dist[s] = 1
    while q:
        temp = q.pop()
        for u in E[temp]:
            if dist[u] >= 0:
                continue
            dist[u] = dist[temp]+1
            q.append(u)
    return dist

for i in range(N):
    if len(E[i]) == 1:
        dist1 = f(i)
        break

num = 0
for i in range(N):
    if num < dist1[i]:
        num = dist1[i]
        idx = i

dist2 = f(idx)
num = max(num, max(dist2))
print(N-num)
0