結果

問題 No.872 All Tree Path
ユーザー 👑 rin204rin204
提出日時 2022-01-19 00:21:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 739 ms / 3,000 ms
コード長 515 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 86,760 KB
実行使用メモリ 302,432 KB
最終ジャッジ日時 2023-08-15 07:46:39
合計ジャッジ時間 9,018 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 552 ms
114,488 KB
testcase_01 AC 551 ms
113,984 KB
testcase_02 AC 739 ms
114,860 KB
testcase_03 AC 699 ms
302,432 KB
testcase_04 AC 126 ms
71,120 KB
testcase_05 AC 573 ms
113,616 KB
testcase_06 AC 602 ms
115,740 KB
testcase_07 AC 594 ms
113,612 KB
testcase_08 AC 142 ms
82,000 KB
testcase_09 AC 142 ms
83,096 KB
testcase_10 AC 139 ms
81,708 KB
testcase_11 AC 146 ms
81,844 KB
testcase_12 AC 138 ms
82,180 KB
testcase_13 AC 62 ms
70,880 KB
testcase_14 AC 64 ms
70,884 KB
testcase_15 AC 65 ms
70,992 KB
testcase_16 AC 64 ms
70,996 KB
testcase_17 AC 63 ms
71,056 KB
testcase_18 AC 64 ms
70,996 KB
testcase_19 AC 63 ms
70,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10 ** 9)

n = int(input())
edges = [[] for _ in range(n)]
for _ in range(n - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    edges[u].append((v, w))
    edges[v].append((u, w))
    
child = [1] * n
ans = 0
def dfs(pos, bpos):
    global ans
    for npos, w in edges[pos]:
        if npos == bpos:
            continue
        dfs(npos, pos)
        ans += child[npos] * (n - child[npos]) * w
        child[pos] += child[npos]
        
dfs(0, -1)
print(ans * 2)
0