結果

問題 No.872 All Tree Path
ユーザー brthyyjpbrthyyjp
提出日時 2020-04-09 09:22:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 772 ms / 3,000 ms
コード長 607 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 86,784 KB
実行使用メモリ 178,984 KB
最終ジャッジ日時 2023-09-28 12:45:31
合計ジャッジ時間 9,704 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 766 ms
178,984 KB
testcase_01 AC 772 ms
178,928 KB
testcase_02 AC 764 ms
178,824 KB
testcase_03 AC 450 ms
165,404 KB
testcase_04 AC 74 ms
71,336 KB
testcase_05 AC 760 ms
177,588 KB
testcase_06 AC 754 ms
178,796 KB
testcase_07 AC 749 ms
178,652 KB
testcase_08 AC 140 ms
85,212 KB
testcase_09 AC 138 ms
85,496 KB
testcase_10 AC 135 ms
85,344 KB
testcase_11 AC 137 ms
85,456 KB
testcase_12 AC 137 ms
85,496 KB
testcase_13 AC 72 ms
71,384 KB
testcase_14 AC 72 ms
71,392 KB
testcase_15 AC 74 ms
71,292 KB
testcase_16 AC 73 ms
70,992 KB
testcase_17 AC 75 ms
71,540 KB
testcase_18 AC 74 ms
71,180 KB
testcase_19 AC 73 ms
71,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

n = int(input())
g = [[] for _ in range(n)]
d = {}
for i in range(n-1):
    u, v, w = map(int, input().split())
    u, v = u-1, v-1
    d[(u, v)] = w
    d[(v, u)] = w
    g[u].append(v)
    g[v].append(u)

s = []
visit = [-1]*n
s.append(0)
order = []
par = [-1]*n
while s:
    v = s.pop()
    order.append(v)
    for u in g[v]:
        if u == par[v]:
            continue
        par[u] = v
        s.append(u)

ans = 0
c = [1]*n
order.reverse()
for v in order:
    if par[v] != -1:
        ans += (n-c[v])*c[v]*d[(v, par[v])]
        c[par[v]] += c[v]
print(ans*2)
0