結果

問題 No.872 All Tree Path
ユーザー anagohirameanagohirame
提出日時 2019-08-30 23:14:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 779 ms / 3,000 ms
コード長 399 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 82,036 KB
実行使用メモリ 318,516 KB
最終ジャッジ日時 2024-05-01 20:35:46
合計ジャッジ時間 6,802 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 586 ms
131,380 KB
testcase_01 AC 579 ms
131,096 KB
testcase_02 AC 779 ms
132,008 KB
testcase_03 AC 575 ms
318,516 KB
testcase_04 AC 36 ms
52,580 KB
testcase_05 AC 611 ms
131,644 KB
testcase_06 AC 600 ms
131,636 KB
testcase_07 AC 593 ms
131,708 KB
testcase_08 AC 113 ms
82,296 KB
testcase_09 AC 119 ms
82,372 KB
testcase_10 AC 112 ms
81,744 KB
testcase_11 AC 122 ms
82,496 KB
testcase_12 AC 111 ms
82,020 KB
testcase_13 AC 34 ms
52,748 KB
testcase_14 AC 35 ms
52,216 KB
testcase_15 AC 32 ms
52,828 KB
testcase_16 AC 34 ms
53,372 KB
testcase_17 AC 36 ms
52,664 KB
testcase_18 AC 34 ms
52,828 KB
testcase_19 AC 34 ms
52,280 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