結果

問題 No.872 All Tree Path
ユーザー maspy
提出日時 2020-01-29 09:58:47
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,149 ms / 3,000 ms
コード長 718 bytes
コンパイル時間 242 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 103,692 KB
最終ジャッジ日時 2024-09-15 20:13:09
合計ジャッジ時間 11,636 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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