結果

問題 No.872 All Tree Path
ユーザー irumo8202irumo8202
提出日時 2022-01-31 10:43:58
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,159 ms / 3,000 ms
コード長 520 bytes
コンパイル時間 136 ms
コンパイル使用メモリ 10,852 KB
実行使用メモリ 119,652 KB
最終ジャッジ日時 2023-09-02 02:06:29
合計ジャッジ時間 12,105 ms
ジャッジサーバーID
(参考情報)
judge16 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,159 ms
66,612 KB
testcase_01 AC 1,141 ms
66,564 KB
testcase_02 AC 1,143 ms
66,644 KB
testcase_03 AC 941 ms
119,652 KB
testcase_04 AC 17 ms
7,856 KB
testcase_05 AC 1,123 ms
66,568 KB
testcase_06 AC 1,129 ms
66,588 KB
testcase_07 AC 1,134 ms
66,540 KB
testcase_08 AC 113 ms
14,144 KB
testcase_09 AC 114 ms
14,112 KB
testcase_10 AC 114 ms
14,208 KB
testcase_11 AC 112 ms
14,096 KB
testcase_12 AC 117 ms
14,108 KB
testcase_13 AC 16 ms
7,780 KB
testcase_14 AC 15 ms
7,908 KB
testcase_15 AC 16 ms
7,900 KB
testcase_16 AC 16 ms
7,908 KB
testcase_17 AC 16 ms
7,780 KB
testcase_18 AC 16 ms
7,852 KB
testcase_19 AC 16 ms
7,788 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(10 ** 7)

N = int(input())

G = [dict() for _ in range(N)]

for _ in range(N - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    G[u][v] = w
    G[v][u] = w

p = -1
ans = 0


def dfs(v, pre=-1):
    global p, ans
    p += 1
    v_in = p
    for nx in G[v].keys():
        if nx == pre:
            continue
        dfs(nx, v)
    p += 1
    v_out = p
    if pre != -1:
        ans += (size := (v_out - v_in) // 2 + 1) * (N - size) * 2 * G[v][pre]


dfs(0)
print(ans)
0