結果

問題 No.872 All Tree Path
ユーザー rin204rin204
提出日時 2022-01-19 00:21:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 702 ms / 3,000 ms
コード長 515 bytes
コンパイル時間 256 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 294,528 KB
最終ジャッジ日時 2024-05-02 19:38:50
合計ジャッジ時間 7,849 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 498 ms
110,592 KB
testcase_01 AC 501 ms
110,848 KB
testcase_02 AC 702 ms
111,360 KB
testcase_03 AC 589 ms
294,528 KB
testcase_04 AC 37 ms
51,584 KB
testcase_05 AC 574 ms
110,592 KB
testcase_06 AC 553 ms
110,848 KB
testcase_07 AC 516 ms
110,464 KB
testcase_08 AC 123 ms
80,128 KB
testcase_09 AC 126 ms
80,128 KB
testcase_10 AC 120 ms
80,384 KB
testcase_11 AC 123 ms
80,384 KB
testcase_12 AC 119 ms
79,872 KB
testcase_13 AC 35 ms
52,096 KB
testcase_14 AC 34 ms
51,712 KB
testcase_15 AC 34 ms
51,712 KB
testcase_16 AC 34 ms
51,712 KB
testcase_17 AC 35 ms
51,712 KB
testcase_18 AC 34 ms
51,968 KB
testcase_19 AC 35 ms
52,224 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