結果

問題 No.1488 Max Score of the Tree
ユーザー gorugo30gorugo30
提出日時 2021-04-23 21:46:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 425 ms / 2,000 ms
コード長 1,220 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 86,796 KB
実行使用メモリ 155,620 KB
最終ジャッジ日時 2023-09-17 12:01:02
合計ジャッジ時間 9,330 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 402 ms
152,112 KB
testcase_01 AC 393 ms
148,852 KB
testcase_02 AC 414 ms
152,728 KB
testcase_03 AC 420 ms
155,404 KB
testcase_04 AC 425 ms
155,504 KB
testcase_05 AC 99 ms
71,496 KB
testcase_06 AC 181 ms
96,300 KB
testcase_07 AC 284 ms
121,500 KB
testcase_08 AC 225 ms
108,064 KB
testcase_09 AC 199 ms
99,072 KB
testcase_10 AC 287 ms
123,136 KB
testcase_11 AC 422 ms
155,620 KB
testcase_12 AC 109 ms
77,604 KB
testcase_13 AC 153 ms
77,264 KB
testcase_14 AC 258 ms
114,052 KB
testcase_15 AC 204 ms
100,212 KB
testcase_16 AC 124 ms
81,288 KB
testcase_17 AC 162 ms
91,400 KB
testcase_18 AC 318 ms
129,068 KB
testcase_19 AC 222 ms
105,776 KB
testcase_20 AC 159 ms
88,544 KB
testcase_21 AC 129 ms
82,424 KB
testcase_22 AC 200 ms
99,452 KB
testcase_23 AC 97 ms
71,756 KB
testcase_24 AC 95 ms
71,740 KB
testcase_25 AC 100 ms
71,804 KB
testcase_26 AC 174 ms
94,424 KB
testcase_27 AC 121 ms
77,720 KB
testcase_28 AC 142 ms
85,172 KB
testcase_29 AC 154 ms
87,980 KB
testcase_30 AC 356 ms
139,936 KB
testcase_31 AC 425 ms
155,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, K = map(int, input().split())
adj = [[] for i in range(N)]
E = []
for i in range(N - 1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    adj[a].append((b, c))
    adj[b].append((a, c))
    E.append((a, b, c))
depth = [-1] * N
depth[0] = 0
from collections import deque
qu = deque([0])
while len(qu):
    v = qu.popleft()
    for nv, c in adj[v]:
        if depth[nv] == -1:
            depth[nv] = depth[v] + c
            qu.append(nv)
dp = [1] * N
for v in range(N):
    for nv, c in adj[v]:
        if depth[nv] > depth[v]:
            dp[v] = 0
            break
ans = sum([dp[i] * depth[i] for i in range(N)])
node = [i for i in range(N)]
node.sort(key = lambda x: depth[x], reverse = True)

for v in node:
    for nv, c in adj[v]:
        if depth[nv] < depth[v]:
            continue
        dp[v] += dp[nv]
V = []
for i in range(N - 1):
    a, b, c = E[i]
    if depth[a] > depth[b]:
        a, b = b, a
    #aが浅い
    V.append(c * dp[b])

dp = [[0] * (K + 1) for i in range(N)]
for i in range(N - 1):
    for j in range(K + 1):
        dp[i + 1][j] = dp[i][j]
        if j + E[i][2] <= K:
            dp[i + 1][j] = max(dp[i + 1][j], dp[i][j + E[i][2]] + V[i])
print(max(dp[-1]) + ans)
0