結果

問題 No.872 All Tree Path
ユーザー lloyzlloyz
提出日時 2022-07-07 22:09:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 769 ms / 3,000 ms
コード長 754 bytes
コンパイル時間 369 ms
コンパイル使用メモリ 87,020 KB
実行使用メモリ 176,464 KB
最終ジャッジ日時 2023-08-26 09:18:05
合計ジャッジ時間 10,242 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 759 ms
176,364 KB
testcase_01 AC 769 ms
176,464 KB
testcase_02 AC 769 ms
176,072 KB
testcase_03 AC 391 ms
173,872 KB
testcase_04 AC 93 ms
71,644 KB
testcase_05 AC 757 ms
175,944 KB
testcase_06 AC 746 ms
176,324 KB
testcase_07 AC 762 ms
176,248 KB
testcase_08 AC 194 ms
86,016 KB
testcase_09 AC 181 ms
84,540 KB
testcase_10 AC 182 ms
84,536 KB
testcase_11 AC 185 ms
84,440 KB
testcase_12 AC 182 ms
84,356 KB
testcase_13 AC 94 ms
71,536 KB
testcase_14 AC 93 ms
71,736 KB
testcase_15 AC 94 ms
71,408 KB
testcase_16 AC 95 ms
71,444 KB
testcase_17 AC 95 ms
71,516 KB
testcase_18 AC 94 ms
71,648 KB
testcase_19 AC 92 ms
71,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict, deque

n = int(input())
edge = defaultdict(list)
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    edge[u].append((v, w))
    edge[v].append((u, w))

ans = 0
Parents = [-1 for _ in range(n)]
visited = set([0])
Que = deque([0])
Topo = []
while Que:
    curr = Que.pop()
    Topo.append(curr)
    for nex, _ in edge[curr]:
        if nex in visited:
            continue
        visited.add(nex)
        Parents[nex] = curr
        Que.append(nex)

Size = [1 for _ in range(n)]
for curr in Topo[::-1]:
    for nex, w in edge[curr]:
        if nex == Parents[curr]:
            continue
        ans += Size[nex] * (n - Size[nex]) * w
        Size[curr] += Size[nex]
print(2 * ans)
0