結果

問題 No.1488 Max Score of the Tree
ユーザー 小野寺健
提出日時 2021-04-24 16:45:23
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 749 bytes
コンパイル時間 72 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 235,712 KB
最終ジャッジ日時 2024-07-04 09:07:34
合計ジャッジ時間 6,463 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other TLE * 1 -- * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

N, K = map(int, input().split())

v = [[[], []] for _ in range(N)]
weight = []
for i in range(N-1):
    a, b, c = map(int, input().split())
    v[a-1][1].append((b-1, i))
    v[b-1][1].append((a-1, i))
    weight.append(c)
    
q = [(0, [])]
e = [0] * (N-1)
while len(q) > 0:
    i, c = q.pop(0)
    v[i][0] = c
    cnt = True
    for j, k in v[i][1]:
        if j == 0 or len(v[j][0]) > 0:
            continue
        cnt = False
        q.append((j, c+[k]))
    if cnt:
        for j in c:
            e[j] += 1         

dp = [[0] * (K+1)]
for i in range(N-1):
    dp.append([max(dp[i][w-weight[i]] + weight[i] * e[i] * 2, dp[i][w] + weight[i] * e[i]) if w >= weight[i] else (dp[i][w] + weight[i] * e[i]) for w in range(K+1)])

print(dp[N-1][K])
0