結果
| 問題 | No.1488 Max Score of the Tree |
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2021-04-23 23:14:17 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,159 bytes |
| 記録 | |
| コンパイル時間 | 89 ms |
| コンパイル使用メモリ | 12,928 KB |
| 実行使用メモリ | 21,268 KB |
| 最終ジャッジ日時 | 2024-07-04 08:43:18 |
| 合計ジャッジ時間 | 3,507 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 22 WA * 7 |
ソースコード
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)
titia