結果

問題 No.872 All Tree Path
ユーザー shinichishinichi
提出日時 2021-09-22 17:08:28
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 675 bytes
コンパイル時間 1,175 ms
コンパイル使用メモリ 87,020 KB
実行使用メモリ 151,340 KB
最終ジャッジ日時 2023-09-18 18:00:25
合計ジャッジ時間 8,037 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 640 ms
149,076 KB
testcase_01 AC 633 ms
148,296 KB
testcase_02 AC 661 ms
149,844 KB
testcase_03 RE -
testcase_04 AC 91 ms
71,796 KB
testcase_05 AC 644 ms
150,640 KB
testcase_06 AC 665 ms
151,340 KB
testcase_07 AC 664 ms
150,900 KB
testcase_08 AC 196 ms
87,572 KB
testcase_09 AC 190 ms
87,432 KB
testcase_10 AC 191 ms
88,184 KB
testcase_11 AC 189 ms
87,472 KB
testcase_12 AC 187 ms
87,156 KB
testcase_13 AC 92 ms
71,412 KB
testcase_14 AC 91 ms
71,796 KB
testcase_15 AC 92 ms
71,504 KB
testcase_16 AC 93 ms
71,404 KB
testcase_17 AC 90 ms
71,404 KB
testcase_18 AC 91 ms
71,472 KB
testcase_19 AC 93 ms
71,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict


N = int(input())
graph = defaultdict(list)
edges = []
for _ in range(N-1):
    a, b, x = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)
    edges.append((a, b, x))

subtree = [0] * N
def dfs(v, p):
    for next_v in graph[v]:
        if next_v == p:
            continue
        dfs(next_v, v)
    subtree[v] = 1
    for next_v in graph[v]:
        subtree[v] += subtree[next_v]

dfs(0, -1)
ans = 0
for a, b, x in edges:
    if subtree[a] < subtree[b]:
        # aよりbの方が根に近いところにある
        a, b = b, a
    ans += x * (subtree[b]) * (N-subtree[b]) * 2
print(ans)
0