結果
| 問題 | No.872 All Tree Path |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-09-22 17:09:06 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 532 ms / 3,000 ms |
| + 331µs | |
| コード長 | 716 bytes |
| 記録 | |
| コンパイル時間 | 225 ms |
| コンパイル使用メモリ | 95,856 KB |
| 実行使用メモリ | 371,444 KB |
| 最終ジャッジ日時 | 2026-08-07 14:36:40 |
| 合計ジャッジ時間 | 6,801 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 18 |
ソースコード
from collections import defaultdict
import sys
sys.setrecursionlimit(1000000)
N = int(input())
graph = defaultdict(list)
edges = []
for _ in range(N-1):
a, b, x = map(int, input().split())
a -= 1
b -= 1
graph[a].append(b)
graph[b].append(a)
edges.append((a, b, x))
subtree = [0] * N
def dfs(v, p):
for next_v in graph[v]:
if next_v == p:
continue
dfs(next_v, v)
subtree[v] = 1
for next_v in graph[v]:
subtree[v] += subtree[next_v]
dfs(0, -1)
ans = 0
for a, b, x in edges:
if subtree[a] < subtree[b]:
# aよりbの方が根に近いところにある
a, b = b, a
ans += x * (subtree[b]) * (N-subtree[b]) * 2
print(ans)