結果
| 問題 | No.872 All Tree Path |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-05-17 23:14:35 |
| 言語 | PyPy3 (7.3.23) |
| 結果 |
AC
|
| 実行時間 | 621 ms / 3,000 ms |
| + 426µs | |
| コード長 | 649 bytes |
| 記録 | |
| コンパイル時間 | 267 ms |
| コンパイル使用メモリ | 95,852 KB |
| 実行使用メモリ | 157,824 KB |
| 最終ジャッジ日時 | 2026-08-28 07:03:08 |
| 合計ジャッジ時間 | 8,219 ms |
|
ジャッジサーバーID (参考情報) |
judge2_1 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
from collections import deque
n = int(input())
e = [[] for i in range(n)]
for _ in range(n-1):
u,v,w = map(int,input().split())
u,v = u-1,v-1
e[u].append([v,w])
e[v].append([u,w])
ans = 0
par = [-1]*n
vis = [0]*n
vis[0] = 1
q = deque([0])
topo = []
while q:
now = q.pop()
topo.append(now)
for nex,_ in e[now]:
if vis[nex]:
continue
vis[nex] = 1
par[nex] = now
q.append(nex)
size = [1]*n
for now in topo[::-1]:
for nex,w in e[now]:
if nex == par[now]:
continue
ans += size[nex]*(n-size[nex])*w
size[now] += size[nex]
print(ans*2)