結果

問題 No.1488 Max Score of the Tree
コンテスト
ユーザー ああ
提出日時 2026-05-15 20:27:11
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 708 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 327 ms
コンパイル使用メモリ 85,376 KB
実行使用メモリ 65,792 KB
最終ジャッジ日時 2026-05-15 20:27:15
合計ジャッジ時間 3,454 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque

n,k=map(int,input().split())
v=[[] for i in range(n)];x=[]
for i in range(n-1):
    a,b,c=map(int,input().split());a-=1;b-=1
    x.append((a,b,c))
    v[a].append(b);v[b].append(a)
dep=[0]*n
f=deque([(0,0,0)])
while f:
    q,w,e=f.pop()
    if len(v[q])>e:
        f.append((q,w,e+1))
        if v[q][e]!=w:
            f.append((v[q][e],q,0))
    else:
        if len(v[q])==1 and v[q][0]==w:
            dep[q]=1
            continue
        for i in v[q]:
            if i!=w:
                dep[q]+=dep[i]
dp=[0]*(k+1)
for i,j,l in x:
    c=min(dep[i],dep[j])
    for j in range(k,-1,-1):
    	if j+l<=k:
    		dp[j+l]=max(dp[j+l],dp[j]+l*2*c)
    	dp[j]+=l*c
print(dp[k])
0