結果

問題 No.872 All Tree Path
コンテスト
ユーザー anagohirame
提出日時 2019-08-30 23:13:51
言語 Python3
(3.14.2 + numpy 2.4.0 + scipy 1.16.3)
結果
AC  
実行時間 2,961 ms / 3,000 ms
コード長 399 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 779 ms
コンパイル使用メモリ 20,680 KB
実行使用メモリ 120,956 KB
最終ジャッジ日時 2026-01-05 11:55:27
合計ジャッジ時間 27,213 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
sys.setrecursionlimit(10**6)
n = int(input())
adj = [[] for _ in range(n)]
for _ in range(n-1):
	u, v, w = map(int, input().split())
	adj[u-1].append([v-1, w])
	adj[v-1].append([u-1, w])

ans = 0
def dfs(x, parent):
	global ans
	child_num = 1
	for c, w in adj[x]:
		if c == parent:
			continue
		d = dfs(c, x)
		ans += 2*d*(n-d)*w
		child_num += d
	return child_num

dfs(0, -1)
print(ans)
0