結果
問題 | No.872 All Tree Path |
ユーザー |
|
提出日時 | 2021-09-22 17:08:28 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 675 bytes |
コンパイル時間 | 139 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 148,728 KB |
最終ジャッジ日時 | 2024-07-05 07:56:17 |
合計ジャッジ時間 | 6,913 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 17 RE * 1 |
ソースコード
from collections import defaultdictN = int(input())graph = defaultdict(list)edges = []for _ in range(N-1):a, b, x = map(int, input().split())a -= 1b -= 1graph[a].append(b)graph[b].append(a)edges.append((a, b, x))subtree = [0] * Ndef dfs(v, p):for next_v in graph[v]:if next_v == p:continuedfs(next_v, v)subtree[v] = 1for next_v in graph[v]:subtree[v] += subtree[next_v]dfs(0, -1)ans = 0for a, b, x in edges:if subtree[a] < subtree[b]:# aよりbの方が根に近いところにあるa, b = b, aans += x * (subtree[b]) * (N-subtree[b]) * 2print(ans)