結果

問題 No.1488 Max Score of the Tree
ユーザー brthyyjpbrthyyjp
提出日時 2021-05-04 22:13:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 183 ms / 2,000 ms
コード長 1,121 bytes
コンパイル時間 321 ms
コンパイル使用メモリ 86,832 KB
実行使用メモリ 77,444 KB
最終ジャッジ日時 2023-09-30 23:48:51
合計ジャッジ時間 5,499 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
76,984 KB
testcase_01 AC 183 ms
77,172 KB
testcase_02 AC 168 ms
77,024 KB
testcase_03 AC 163 ms
76,948 KB
testcase_04 AC 170 ms
77,032 KB
testcase_05 AC 69 ms
71,092 KB
testcase_06 AC 92 ms
77,156 KB
testcase_07 AC 127 ms
77,296 KB
testcase_08 AC 98 ms
77,072 KB
testcase_09 AC 111 ms
76,708 KB
testcase_10 AC 115 ms
76,916 KB
testcase_11 AC 168 ms
77,444 KB
testcase_12 AC 74 ms
75,992 KB
testcase_13 AC 97 ms
76,472 KB
testcase_14 AC 113 ms
76,956 KB
testcase_15 AC 98 ms
77,004 KB
testcase_16 AC 76 ms
76,352 KB
testcase_17 AC 89 ms
77,028 KB
testcase_18 AC 129 ms
76,760 KB
testcase_19 AC 100 ms
77,260 KB
testcase_20 AC 89 ms
76,732 KB
testcase_21 AC 77 ms
76,284 KB
testcase_22 AC 98 ms
77,140 KB
testcase_23 AC 72 ms
70,908 KB
testcase_24 AC 69 ms
71,072 KB
testcase_25 AC 70 ms
71,240 KB
testcase_26 AC 87 ms
77,204 KB
testcase_27 AC 79 ms
75,992 KB
testcase_28 AC 86 ms
76,652 KB
testcase_29 AC 90 ms
76,780 KB
testcase_30 AC 130 ms
77,168 KB
testcase_31 AC 164 ms
77,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

n, k = map(int, input().split())
g = [[] for i in range(n)]
toc = {}
edge = []
for i in range(n-1):
    a, b, c = map(int, input().split())
    a, b = a-1, b-1
    g[a].append(b)
    g[b].append(a)
    toc[(a, b)] = c
    toc[(b, a)] = c
    edge.append((a, b, c))

s = []
s.append(0)
parent = [-1]*n
order = []
while s:
    v = s.pop()
    order.append(v)
    for u in g[v]:
        if parent[v] != u:
            s.append(u)
            parent[u] = v
order.reverse()
leaf = [0]*n
for v in order:
    if len(g[v]) == 1:
        leaf[v] = 1
    p =  parent[v]
    if p != -1:
        leaf[p] += leaf[v]
dp1 = [0]*n
for v in order:
    p =  parent[v]
    if p != -1:
        dp1[p] += dp1[v]+toc[(p, v)]*leaf[v]
#print(dp1)
dp2 = [-1]*(k+1)
dp2[0] = 0
for a, b, c in edge:
    if parent[a] == b:
        v = a
    else:
        v = b
    for i in reversed(range(k+1)):
        if dp2[i] == -1:
            continue
        if i+c <= k:
            dp2[i+c] = max(dp2[i+c], dp2[i]+c*leaf[v])
#print(dp2)
ans = dp1[0]+max(dp2)
print(ans)
0