結果

問題 No.872 All Tree Path
ユーザー anagohirameanagohirame
提出日時 2019-08-30 23:15:55
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,259 ms / 3,000 ms
コード長 400 bytes
コンパイル時間 220 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 115,968 KB
最終ジャッジ日時 2024-05-01 20:38:20
合計ジャッジ時間 19,307 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,231 ms
75,520 KB
testcase_01 AC 2,239 ms
75,392 KB
testcase_02 AC 2,225 ms
75,264 KB
testcase_03 AC 1,338 ms
115,968 KB
testcase_04 AC 29 ms
10,752 KB
testcase_05 AC 2,240 ms
75,392 KB
testcase_06 AC 2,259 ms
75,392 KB
testcase_07 AC 2,222 ms
75,392 KB
testcase_08 AC 149 ms
17,280 KB
testcase_09 AC 154 ms
17,280 KB
testcase_10 AC 149 ms
17,280 KB
testcase_11 AC 150 ms
17,280 KB
testcase_12 AC 151 ms
17,152 KB
testcase_13 AC 28 ms
10,752 KB
testcase_14 AC 28 ms
10,752 KB
testcase_15 AC 28 ms
10,752 KB
testcase_16 AC 28 ms
10,752 KB
testcase_17 AC 28 ms
10,752 KB
testcase_18 AC 28 ms
10,752 KB
testcase_19 AC 28 ms
10,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(400000)
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