結果

問題 No.806 木を道に
ユーザー flippergoflippergo
提出日時 2020-12-30 19:10:35
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 870 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 94,592 KB
最終ジャッジ日時 2024-04-16 20:00:23
合計ジャッジ時間 5,294 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
54,016 KB
testcase_01 AC 39 ms
53,632 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 38 ms
53,888 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 40 ms
53,888 KB
testcase_08 AC 41 ms
54,144 KB
testcase_09 AC 42 ms
53,376 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 133 ms
88,708 KB
testcase_25 AC 163 ms
94,240 KB
testcase_26 AC 110 ms
83,208 KB
testcase_27 AC 177 ms
94,336 KB
testcase_28 AC 89 ms
77,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
N = int(input())
G = {i:[] for i in range(1,N+1)}
for _ in range(N-1):
    a,b = map(int,input().split())
    G[a].append(b)
    G[b].append(a)
d1 = [-1 for _ in range(N+1)]
d1[1] = 0
que = deque([(1,0)])
while que:
    x,d = que.popleft()
    for y in G[x]:
        if d1[y]<0:
            d1[y]=d+1
            que.append((y,d+1))
dmax = 0
for i in range(1,N+1):
    if dmax<d1[i]:
        dmax = d1[i]
        ind = i
d2 = [-1 for _ in range(N+1)]
d2[ind]=0
que = deque([(ind,0)])
P = [0 for _ in range(N+1)]
while que:
    x,d = que.popleft()
    for y in G[x]:
        if d2[y]<0:
            d2[y]=d+1
            P[y] = x
            que.append((y,d+1))
dmax = max(d2)
ind1 = d2.index(dmax)
ind0 = ind
ind = ind1
A = []
while ind!=ind0:
    ind = P[ind]
    A.append(ind)
A.pop()
cnt = 0
for a in A:
    cnt += len(G[a])-2
print(cnt)
0