結果

問題 No.1488 Max Score of the Tree
ユーザー hir355hir355
提出日時 2021-04-23 22:27:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 334 ms / 2,000 ms
コード長 996 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 155,436 KB
最終ジャッジ日時 2023-09-17 12:35:00
合計ジャッジ時間 7,624 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 322 ms
151,880 KB
testcase_01 AC 307 ms
148,608 KB
testcase_02 AC 329 ms
152,448 KB
testcase_03 AC 330 ms
155,320 KB
testcase_04 AC 334 ms
155,392 KB
testcase_05 AC 75 ms
71,312 KB
testcase_06 AC 142 ms
95,980 KB
testcase_07 AC 226 ms
121,700 KB
testcase_08 AC 180 ms
107,736 KB
testcase_09 AC 156 ms
98,468 KB
testcase_10 AC 229 ms
122,848 KB
testcase_11 AC 328 ms
155,436 KB
testcase_12 AC 85 ms
76,712 KB
testcase_13 AC 125 ms
88,112 KB
testcase_14 AC 201 ms
113,880 KB
testcase_15 AC 166 ms
99,444 KB
testcase_16 AC 97 ms
80,564 KB
testcase_17 AC 127 ms
90,968 KB
testcase_18 AC 251 ms
128,824 KB
testcase_19 AC 175 ms
105,520 KB
testcase_20 AC 120 ms
87,832 KB
testcase_21 AC 103 ms
81,996 KB
testcase_22 AC 157 ms
98,840 KB
testcase_23 AC 73 ms
71,384 KB
testcase_24 AC 76 ms
71,612 KB
testcase_25 AC 74 ms
71,280 KB
testcase_26 AC 134 ms
94,232 KB
testcase_27 AC 89 ms
76,120 KB
testcase_28 AC 109 ms
84,448 KB
testcase_29 AC 116 ms
87,252 KB
testcase_30 AC 283 ms
139,588 KB
testcase_31 AC 331 ms
155,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def tree_dfs(g, root=0):
    s = [root]
    d = [True] * len(g)
    d[root] = False
    order = []
    while s:
        p = s.pop()
        order.append(p)
        d[p] = False
        for node, _ in g[p]:
            if d[node]:
                s.append(node)
    return order

n, k = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(n - 1):
    a, b, c = map(int, input().split())
    g[a - 1].append((b - 1, c))
    g[b - 1].append((a - 1, c))
order = tree_dfs(g)
d = [-1] * n
a = [0] * n
b = [0] * n
for v in order[::-1]:
    d[v] = 0
    for node, cost in g[v]:
        if d[node] != -1:
            d[v] += d[node]
            a[node] += d[node] * cost
            b[node] = cost
    if d[v] == 0:
        d[v] = 1
dp = [[0] * (k + 1) for _ in range(n + 1)]
for i in range(n):
    for j in range(k + 1):
        if j - b[i] >= 0:
            dp[i + 1][j] = max(dp[i][j - b[i]] + a[i], dp[i][j])
        else:
            dp[i + 1][j] = dp[i][j]
print(max(dp[n]) + sum(a))
0