結果

問題 No.872 All Tree Path
ユーザー shinichishinichi
提出日時 2021-09-22 17:09:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 940 ms / 3,000 ms
コード長 716 bytes
コンパイル時間 573 ms
コンパイル使用メモリ 86,992 KB
実行使用メモリ 391,560 KB
最終ジャッジ日時 2023-09-18 18:01:06
合計ジャッジ時間 10,457 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 715 ms
149,372 KB
testcase_01 AC 700 ms
148,452 KB
testcase_02 AC 702 ms
150,248 KB
testcase_03 AC 940 ms
391,560 KB
testcase_04 AC 216 ms
71,576 KB
testcase_05 AC 776 ms
150,716 KB
testcase_06 AC 790 ms
151,420 KB
testcase_07 AC 747 ms
150,612 KB
testcase_08 AC 203 ms
87,288 KB
testcase_09 AC 200 ms
87,812 KB
testcase_10 AC 199 ms
88,144 KB
testcase_11 AC 201 ms
87,248 KB
testcase_12 AC 205 ms
87,344 KB
testcase_13 AC 94 ms
71,688 KB
testcase_14 AC 95 ms
71,596 KB
testcase_15 AC 94 ms
71,752 KB
testcase_16 AC 93 ms
71,792 KB
testcase_17 AC 94 ms
71,244 KB
testcase_18 AC 95 ms
71,780 KB
testcase_19 AC 96 ms
71,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import sys
sys.setrecursionlimit(1000000)

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