結果

問題 No.872 All Tree Path
ユーザー maspymaspy
提出日時 2020-01-29 09:58:47
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,158 ms / 3,000 ms
コード長 718 bytes
コンパイル時間 1,027 ms
コンパイル使用メモリ 10,924 KB
実行使用メモリ 102,308 KB
最終ジャッジ日時 2023-10-14 00:22:59
合計ジャッジ時間 12,437 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,132 ms
102,232 KB
testcase_01 AC 1,130 ms
102,116 KB
testcase_02 AC 1,129 ms
102,136 KB
testcase_03 AC 633 ms
102,308 KB
testcase_04 AC 16 ms
7,740 KB
testcase_05 AC 1,158 ms
101,344 KB
testcase_06 AC 1,152 ms
101,432 KB
testcase_07 AC 1,139 ms
101,404 KB
testcase_08 AC 93 ms
17,500 KB
testcase_09 AC 96 ms
17,488 KB
testcase_10 AC 93 ms
17,500 KB
testcase_11 AC 96 ms
17,356 KB
testcase_12 AC 98 ms
17,344 KB
testcase_13 AC 16 ms
7,828 KB
testcase_14 AC 16 ms
7,732 KB
testcase_15 AC 16 ms
7,728 KB
testcase_16 AC 16 ms
7,724 KB
testcase_17 AC 16 ms
7,784 KB
testcase_18 AC 16 ms
7,732 KB
testcase_19 AC 16 ms
7,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
m = map(int,read().split())
UVW = zip(m,m,m)

graph = [[] for _ in range(N+1)]
for u,v,w in UVW:
    graph[u].append((v,w))
    graph[v].append((u,w))

root = 1
parent = [0] * (N+1)
length = [0] * (N+1)
order = []
stack = [root]
while stack:
    x = stack.pop()
    order.append(x)
    for y,d in graph[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        length[y] = d
        stack.append(y)

size = [1] * (N+1)
for v in order[::-1]:
    p = parent[v]
    size[p] += size[v]

answer = 2 * sum(d * x * (N-x) for d,x in zip(length, size))
print(answer)
0