結果

問題 No.1488 Max Score of the Tree
ユーザー monnumonnu
提出日時 2021-04-23 23:31:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 614 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 10,996 KB
実行使用メモリ 16,644 KB
最終ジャッジ日時 2023-09-17 13:12:32
合計ジャッジ時間 9,208 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

def dfs(G,v,p,sz,edge):
    cnt=0
    for nv,c in G[v]:
        if nv==p:
            continue
        cnt+=1
        dfs(G,nv,v,sz,edge)
        sz[v]+=sz[nv]
        edge.append([c,sz[nv]])
    if cnt==0:
        sz[v]=1


N,K=map(int,input().split())
G=[[] for i in range(N)]
for _ in range(N-1):
    a,b,c=map(int,input().split())
    a-=1
    b-=1
    G[a].append([b,c])
    G[b].append([a,c])

sz=[0]*N
edge=[]
dfs(G,0,-1,sz,edge)
ans=0
dp=[0]*(K+1)
for e,cnt in edge:
    ans+=e*cnt
    for i in range(K,-1,-1):
        if i-e<0:
            break;
        dp[i]=max(dp[i],dp[i-e]+cnt*e)
print(ans+max(dp))
0