結果
| 問題 |
No.1488 Max Score of the Tree
|
| コンテスト | |
| ユーザー |
Kiri8128
|
| 提出日時 | 2021-04-23 23:35:01 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 91 ms / 2,000 ms |
| コード長 | 761 bytes |
| コンパイル時間 | 178 ms |
| コンパイル使用メモリ | 81,920 KB |
| 実行使用メモリ | 63,488 KB |
| 最終ジャッジ日時 | 2024-07-04 08:48:43 |
| 合計ジャッジ時間 | 3,203 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
from collections import deque
N, K = map(int, input().split())
X = [[] for i in range(N)]
for i in range(N-1):
x, y, c= map(int, input().split())
x, y = x-1, y-1 # 1-indexed
X[x].append((y, c))
X[y].append((x, c))
P = [-1] * N
Q = deque([0])
R = []
S = [0] * N
while Q:
i = deque.popleft(Q)
R.append(i)
for a, c in X[i]:
if a != P[i]:
P[a] = i
S[a] = c
X[a].remove((i, c))
deque.append(Q, a)
T = [0] * N
ans = 0
Y = []
for i in R[1:][::-1]:
if not len(X[i]): T[i] += 1
Y.append((S[i], S[i] * T[i]))
ans += S[i] * T[i]
T[P[i]] += T[i]
Z = [0] * (K + 1)
for y, v in Y:
for i in range(K, y-1, -1):
Z[i] = max(Z[i], Z[i-y] + v)
ans += Z[K]
print(ans)
Kiri8128