結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 534 ms
131,296 KB
testcase_01 AC 494 ms
131,248 KB
testcase_02 AC 690 ms
131,556 KB
testcase_03 AC 518 ms
310,504 KB
testcase_04 AC 36 ms
52,804 KB
testcase_05 AC 521 ms
131,252 KB
testcase_06 AC 517 ms
131,648 KB
testcase_07 AC 517 ms
131,280 KB
testcase_08 AC 97 ms
81,892 KB
testcase_09 AC 96 ms
81,752 KB
testcase_10 AC 94 ms
81,620 KB
testcase_11 AC 103 ms
81,864 KB
testcase_12 AC 100 ms
81,740 KB
testcase_13 AC 33 ms
52,284 KB
testcase_14 AC 33 ms
52,756 KB
testcase_15 AC 33 ms
52,936 KB
testcase_16 AC 32 ms
53,224 KB
testcase_17 AC 37 ms
52,488 KB
testcase_18 AC 33 ms
52,956 KB
testcase_19 AC 35 ms
53,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(300000)
def input():
	return sys.stdin.readline()[:-1]

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