結果

問題 No.1488 Max Score of the Tree
ユーザー Kiri8128Kiri8128
提出日時 2021-04-23 23:35:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 761 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 87,172 KB
実行使用メモリ 78,060 KB
最終ジャッジ日時 2023-09-17 13:13:26
合計ジャッジ時間 5,408 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
77,824 KB
testcase_01 AC 138 ms
77,668 KB
testcase_02 AC 143 ms
77,712 KB
testcase_03 AC 141 ms
77,932 KB
testcase_04 AC 141 ms
77,604 KB
testcase_05 AC 97 ms
71,448 KB
testcase_06 AC 113 ms
77,588 KB
testcase_07 AC 126 ms
77,420 KB
testcase_08 AC 117 ms
77,684 KB
testcase_09 AC 115 ms
77,320 KB
testcase_10 AC 122 ms
77,784 KB
testcase_11 AC 125 ms
78,060 KB
testcase_12 AC 98 ms
77,280 KB
testcase_13 AC 115 ms
77,156 KB
testcase_14 AC 113 ms
77,448 KB
testcase_15 AC 112 ms
77,300 KB
testcase_16 AC 103 ms
77,096 KB
testcase_17 AC 109 ms
77,520 KB
testcase_18 AC 117 ms
77,588 KB
testcase_19 AC 113 ms
77,692 KB
testcase_20 AC 106 ms
77,244 KB
testcase_21 AC 102 ms
77,444 KB
testcase_22 AC 112 ms
77,404 KB
testcase_23 AC 91 ms
71,804 KB
testcase_24 AC 93 ms
71,448 KB
testcase_25 AC 94 ms
71,440 KB
testcase_26 AC 109 ms
77,900 KB
testcase_27 AC 103 ms
77,036 KB
testcase_28 AC 108 ms
76,944 KB
testcase_29 AC 106 ms
77,104 KB
testcase_30 AC 131 ms
77,652 KB
testcase_31 AC 145 ms
77,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
N, K = map(int, input().split())
X = [[] for i in range(N)]
for i in range(N-1):
    x, y, c= map(int, input().split())
    x, y = x-1, y-1 # 1-indexed
    X[x].append((y, c))
    X[y].append((x, c))

P = [-1] * N
Q = deque([0])
R = []
S = [0] * N
while Q:
    i = deque.popleft(Q)
    R.append(i)
    for a, c in X[i]:
        if a != P[i]:
            P[a] = i
            S[a] = c
            X[a].remove((i, c))
            deque.append(Q, a)

T = [0] * N
ans = 0
Y = []
for i in R[1:][::-1]:
    if not len(X[i]): T[i] += 1
    Y.append((S[i], S[i] * T[i]))
    ans += S[i] * T[i]
    T[P[i]] += T[i]

Z = [0] * (K + 1)
for y, v in Y:
    for i in range(K, y-1, -1):
        Z[i] = max(Z[i], Z[i-y] + v)
ans += Z[K]
print(ans)
0