結果

問題 No.872 All Tree Path
ユーザー tktk_snsntktk_snsn
提出日時 2021-02-07 19:46:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,199 ms / 3,000 ms
コード長 614 bytes
コンパイル時間 191 ms
コンパイル使用メモリ 10,856 KB
実行使用メモリ 80,060 KB
最終ジャッジ日時 2023-09-17 18:11:23
合計ジャッジ時間 13,556 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,177 ms
75,688 KB
testcase_01 AC 1,170 ms
75,744 KB
testcase_02 AC 1,188 ms
75,748 KB
testcase_03 AC 706 ms
80,060 KB
testcase_04 AC 16 ms
7,916 KB
testcase_05 AC 1,199 ms
75,572 KB
testcase_06 AC 1,187 ms
75,736 KB
testcase_07 AC 1,175 ms
75,732 KB
testcase_08 AC 97 ms
14,912 KB
testcase_09 AC 98 ms
14,936 KB
testcase_10 AC 100 ms
15,024 KB
testcase_11 AC 99 ms
14,960 KB
testcase_12 AC 97 ms
14,908 KB
testcase_13 AC 16 ms
7,748 KB
testcase_14 AC 16 ms
7,752 KB
testcase_15 AC 16 ms
7,740 KB
testcase_16 AC 16 ms
7,808 KB
testcase_17 AC 16 ms
7,756 KB
testcase_18 AC 16 ms
7,924 KB
testcase_19 AC 16 ms
7,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

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


par = [-1] * N
L = [0] * N
topo = []
q = [0]
while q:
    s = q.pop()
    topo.append(s)
    for t, c in G[s]:
        if t == par[s]:
            continue
        par[t] = s
        L[t] = c
        q.append(t)

size = [1] * N
ans = 0
for v in topo[::-1][:-1]:
    sz = size[v]
    ans += L[v] * sz * (N - sz)
    p = par[v]
    size[p] += sz
print(ans * 2)
0