結果

問題 No.872 All Tree Path
ユーザー anagohirameanagohirame
提出日時 2019-08-30 23:13:51
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,068 ms / 3,000 ms
コード長 399 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 10,784 KB
実行使用メモリ 113,192 KB
最終ジャッジ日時 2023-08-14 07:59:28
合計ジャッジ時間 18,341 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,053 ms
72,880 KB
testcase_01 AC 2,053 ms
72,736 KB
testcase_02 AC 2,068 ms
72,700 KB
testcase_03 AC 1,091 ms
113,192 KB
testcase_04 AC 14 ms
7,796 KB
testcase_05 AC 2,047 ms
72,752 KB
testcase_06 AC 2,046 ms
72,872 KB
testcase_07 AC 2,042 ms
72,716 KB
testcase_08 AC 113 ms
14,624 KB
testcase_09 AC 114 ms
14,580 KB
testcase_10 AC 114 ms
14,624 KB
testcase_11 AC 114 ms
14,560 KB
testcase_12 AC 115 ms
14,556 KB
testcase_13 AC 14 ms
7,724 KB
testcase_14 AC 13 ms
7,760 KB
testcase_15 AC 14 ms
7,760 KB
testcase_16 AC 14 ms
7,816 KB
testcase_17 AC 14 ms
7,816 KB
testcase_18 AC 15 ms
7,768 KB
testcase_19 AC 14 ms
7,728 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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