結果
問題 |
No.872 All Tree Path
|
ユーザー |
![]() |
提出日時 | 2021-02-07 19:46:48 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 1,230 ms / 3,000 ms |
コード長 | 614 bytes |
コンパイル時間 | 162 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 82,776 KB |
最終ジャッジ日時 | 2024-07-04 12:43:58 |
合計ジャッジ時間 | 11,866 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 18 |
ソースコード
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)