結果

問題 No.1488 Max Score of the Tree
ユーザー SHIJOU
提出日時 2021-05-19 13:24:37
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 904 bytes
コンパイル時間 369 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 14,720 KB
最終ジャッジ日時 2024-10-09 17:33:13
合計ジャッジ時間 28,235 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

n,k = map(int, input().split())
G = [[] for _ in range(n)]
for i 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))
check = [0]*n
check[0] = 1
stack = [0]
for i in range(n-1):
  for to,cost in G[stack[i]]:
    if check[to]:
      continue
    check[to] = 1
    stack.append(to)
dist = [[0,0] for _ in range(n)]
for i in range(n-1, -1, -1):
  check[stack[i]] = 0
  ex = 1
  for to,cost in G[stack[i]]:
    if check[to] == 1:
      continue
    ex = 0
    dist[stack[i]][0] += dist[to][0]
    dist[stack[i]][1] += dist[to][1] + dist[to][0]*cost
  dist[stack[i]][0] += ex
dp = [0]*(k+1)
for i in range(n):
  check[stack[i]] = 1
  for to,cost in G[stack[i]]:
    if check[to]:
      continue
    for j in range(k, cost-1, -1):
      if dp[j] < dp[j-cost] + cost*dist[to][0]:
        dp[j] = dp[j-cost] + cost*dist[to][0]
print(dist[0][1] + max(dp))
0