結果
問題 | No.1488 Max Score of the Tree |
ユーザー |
![]() |
提出日時 | 2021-04-23 22:41:43 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 1,480 ms / 2,000 ms |
コード長 | 1,233 bytes |
コンパイル時間 | 239 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 14,080 KB |
最終ジャッジ日時 | 2024-07-04 08:26:04 |
合計ジャッジ時間 | 15,478 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
import syssys.setrecursionlimit(10 ** 6)int1 = lambda x: int(x) - 1p2D = lambda x: print(*x, sep="\n")def II(): return int(sys.stdin.buffer.readline())def LI(): return list(map(int, sys.stdin.buffer.readline().split()))def LLI(rows_number): return [LI() for _ in range(rows_number)]def LI1(): return list(map(int1, sys.stdin.buffer.readline().split()))def LLI1(rows_number): return [LI1() for _ in range(rows_number)]def BI(): return sys.stdin.buffer.readline().rstrip()def SI(): return sys.stdin.buffer.readline().rstrip().decode()inf = 10 ** 16md = 10 ** 9 + 7# md = 998244353n,k=LI()to=[[] for _ in range(n)]cc=[]for i in range(n-1):u,v,c=LI()u,v=u-1,v-1to[u].append((v,c,i))to[v].append((u,c,i))cc.append(c)leaf=[0]*netou=[0]*ndef dfs(u=0,pu=-1,s=0):if u and len(to[u])==1:leaf[u]=1return sres=0for v,c,e in to[u]:if v==pu:continueetou[e]=vres+=dfs(v,u,s+c)leaf[u]+=leaf[v]return resscore=dfs()dp=[-1]*(k+1)dp[0]=0for e in range(n-1):c=cc[e]u=etou[e]for i in range(k-c,-1,-1):if dp[i]==-1:continueni=i+cdp[ni]=max(dp[ni],dp[i]+c*leaf[u])print(score+max(dp))