結果

問題 No.1488 Max Score of the Tree
ユーザー titiatitia
提出日時 2021-04-23 23:14:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,159 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 10,896 KB
実行使用メモリ 18,644 KB
最終ジャッジ日時 2023-09-17 13:06:05
合計ジャッジ時間 3,411 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 17 ms
8,044 KB
testcase_04 AC 18 ms
8,180 KB
testcase_05 AC 17 ms
8,152 KB
testcase_06 AC 21 ms
8,968 KB
testcase_07 AC 221 ms
13,796 KB
testcase_08 AC 20 ms
8,112 KB
testcase_09 AC 205 ms
10,996 KB
testcase_10 AC 74 ms
11,324 KB
testcase_11 AC 547 ms
18,644 KB
testcase_12 AC 17 ms
8,180 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 17 ms
8,188 KB
testcase_16 WA -
testcase_17 AC 17 ms
8,108 KB
testcase_18 AC 17 ms
8,032 KB
testcase_19 WA -
testcase_20 AC 17 ms
8,188 KB
testcase_21 AC 17 ms
8,096 KB
testcase_22 AC 17 ms
8,116 KB
testcase_23 AC 16 ms
8,100 KB
testcase_24 AC 16 ms
8,112 KB
testcase_25 AC 17 ms
8,176 KB
testcase_26 AC 16 ms
8,064 KB
testcase_27 AC 18 ms
8,032 KB
testcase_28 AC 28 ms
9,108 KB
testcase_29 AC 17 ms
8,040 KB
testcase_30 AC 17 ms
8,160 KB
testcase_31 AC 266 ms
13,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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

E=[tuple(map(int,input().split())) for i in range(N-1)]

EDGE=[[] for i in range(N)]

for a,b,e in E:
    a-=1
    b-=1
    EDGE[a].append((b,e))
    EDGE[b].append((a,e))

QUE=[0] 
Parent=[-1]*(N+1)
Parent[0]=0
TOP_SORT=[]

while QUE:
    x=QUE.pop()
    TOP_SORT.append(x)
    for to,w in EDGE[x]:
        if Parent[to]==-1:
            Parent[to]=x,w
            QUE.append(to)

EW=[0]*N

for n in TOP_SORT[1:][::-1]:
    if EW[n]==0:
        EW[n]=1
    EW[Parent[n][0]]+=EW[n]

XX=[]
for i in range(1,N):
    XX.append((Parent[i][1],EW[i]))

XC=[[] for i in range(101)]

for x,c in XX:
    XC[c].append(x)

ANS=0
cost=0

for i in range(100,-1,-1):
    if sum(XC[i])+cost<=K:
        ANS+=sum(XC[i])*i*2
        cost+=sum(XC[i])
    else:
        rest=K-cost
        DP={0}

        for j in XC[i]:
            NDP=DP.copy()
            for d in DP:
                if d+j<=rest:
                    NDP.add(d+j)
            DP=NDP

        MAX=max(DP)
        SUM=sum(XC[i])
        ANS+=((SUM-MAX)+MAX*2)*i
        cost+=MAX
print(ANS)
        
            
            
            
0