結果
問題 | No.1488 Max Score of the Tree |
ユーザー |
![]() |
提出日時 | 2022-12-13 00:03:47 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 266 ms / 2,000 ms |
コード長 | 977 bytes |
コンパイル時間 | 309 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 142,848 KB |
最終ジャッジ日時 | 2024-11-06 21:37:29 |
合計ジャッジ時間 | 6,110 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
import sys sys.setrecursionlimit(10**8) import pypyjit pypyjit.set_param("max_unroll_recursion=-1") input = sys.stdin.readline N, K = map(int, input().split()) G = [list() 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)) leaf = [0]*N A = [] score = 0 def dfs(v, p): for next_v,_ in G[v]: if next_v == p: continue dfs(next_v, v) if len(G[v])==1 and v!=0: leaf[v] = 1 else: leaf[v] = 0 for c,cost in G[v]: if c== p: continue leaf[v] += leaf[c] A.append([cost,leaf[c]*cost]) global score score += cost*leaf[c] dfs(0,-1) dp = [0]*(K+1) for i in range(N-1): ndp = [0]*(K+1) w, v = A[i] for j in range(K+1): ndp[j] = max(ndp[j],dp[j]) if j-w>=0: ndp[j] = max(ndp[j],dp[j-w]+v) dp = ndp print(score+dp[-1])