結果
問題 | No.872 All Tree Path |
ユーザー | aaaaaaaaaa2230 |
提出日時 | 2022-05-17 23:14:35 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 883 ms / 3,000 ms |
コード長 | 649 bytes |
コンパイル時間 | 288 ms |
コンパイル使用メモリ | 87,044 KB |
実行使用メモリ | 147,356 KB |
最終ジャッジ日時 | 2023-10-14 02:12:57 |
合計ジャッジ時間 | 10,936 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 883 ms
145,580 KB |
testcase_01 | AC | 880 ms
145,564 KB |
testcase_02 | AC | 872 ms
147,356 KB |
testcase_03 | AC | 377 ms
147,128 KB |
testcase_04 | AC | 92 ms
71,540 KB |
testcase_05 | AC | 880 ms
147,152 KB |
testcase_06 | AC | 856 ms
147,108 KB |
testcase_07 | AC | 878 ms
147,144 KB |
testcase_08 | AC | 188 ms
82,564 KB |
testcase_09 | AC | 186 ms
82,728 KB |
testcase_10 | AC | 187 ms
82,576 KB |
testcase_11 | AC | 182 ms
82,568 KB |
testcase_12 | AC | 184 ms
82,580 KB |
testcase_13 | AC | 91 ms
71,644 KB |
testcase_14 | AC | 91 ms
71,372 KB |
testcase_15 | AC | 90 ms
71,696 KB |
testcase_16 | AC | 96 ms
71,544 KB |
testcase_17 | AC | 95 ms
71,788 KB |
testcase_18 | AC | 92 ms
71,560 KB |
testcase_19 | AC | 94 ms
71,616 KB |
ソースコード
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)