結果

問題 No.806 木を道に
ユーザー hedwig100hedwig100
提出日時 2020-04-22 22:07:51
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 862 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 30,868 KB
最終ジャッジ日時 2024-04-20 10:20:42
合計ジャッジ時間 7,706 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,752 KB
testcase_01 AC 30 ms
10,880 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 28 ms
10,752 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 27 ms
10,752 KB
testcase_08 AC 28 ms
10,624 KB
testcase_09 AC 28 ms
10,752 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 292 ms
21,760 KB
testcase_25 AC 417 ms
27,008 KB
testcase_26 AC 145 ms
16,512 KB
testcase_27 AC 439 ms
29,312 KB
testcase_28 AC 40 ms
11,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


def max_depth(s,return_dist = False):
    ans = 0
    idx = 0
    depth = [-1] * n
    depth[s] = 0
    stack = [s]
    while stack:
        p = stack.pop()
        for v in G[p]:
            if  depth[v] >= 0:
                continue
            depth[v] = depth[p] + 1
            if ans < depth[v]:
                ans = depth[v]
                idx = v
            stack.append(v)
    
    if return_dist:
        return ans,idx,depth
    return ans,idx


depth,s = max_depth(0)
max_dep,i,dist = max_depth(s,True)
d = max_dep
ans = 0
while d > 1:
    for e in G[i]:
        if dist[e] == d - 1:
            i = e
            ans += len(G[e]) - 2
            d -= 1
            break
print(ans)
0