結果
問題 | No.1488 Max Score of the Tree |
ユーザー | HIcoder |
提出日時 | 2023-07-21 21:32:29 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 82 ms / 2,000 ms |
コード長 | 2,393 bytes |
コンパイル時間 | 1,231 ms |
コンパイル使用メモリ | 99,640 KB |
実行使用メモリ | 82,552 KB |
最終ジャッジ日時 | 2023-10-21 21:23:14 |
合計ジャッジ時間 | 4,080 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge13 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 76 ms
79,120 KB |
testcase_01 | AC | 76 ms
75,952 KB |
testcase_02 | AC | 79 ms
79,912 KB |
testcase_03 | AC | 82 ms
82,552 KB |
testcase_04 | AC | 81 ms
82,552 KB |
testcase_05 | AC | 2 ms
4,348 KB |
testcase_06 | AC | 20 ms
23,152 KB |
testcase_07 | AC | 47 ms
48,760 KB |
testcase_08 | AC | 29 ms
35,032 KB |
testcase_09 | AC | 26 ms
25,792 KB |
testcase_10 | AC | 45 ms
50,080 KB |
testcase_11 | AC | 81 ms
82,552 KB |
testcase_12 | AC | 3 ms
4,348 KB |
testcase_13 | AC | 15 ms
14,704 KB |
testcase_14 | AC | 39 ms
41,104 KB |
testcase_15 | AC | 23 ms
26,848 KB |
testcase_16 | AC | 6 ms
7,840 KB |
testcase_17 | AC | 16 ms
17,872 KB |
testcase_18 | AC | 53 ms
56,152 KB |
testcase_19 | AC | 29 ms
32,656 KB |
testcase_20 | AC | 14 ms
15,232 KB |
testcase_21 | AC | 7 ms
9,424 KB |
testcase_22 | AC | 25 ms
26,320 KB |
testcase_23 | AC | 2 ms
4,348 KB |
testcase_24 | AC | 2 ms
4,348 KB |
testcase_25 | AC | 2 ms
4,348 KB |
testcase_26 | AC | 17 ms
21,304 KB |
testcase_27 | AC | 4 ms
5,992 KB |
testcase_28 | AC | 11 ms
11,800 KB |
testcase_29 | AC | 14 ms
14,704 KB |
testcase_30 | AC | 61 ms
66,976 KB |
testcase_31 | AC | 80 ms
82,552 KB |
ソースコード
#include<iostream> #include<set> #include<map> #include<vector> #include<queue> #include<algorithm> #include<tuple> using namespace std; typedef pair<int,int> P; typedef long long ll; const ll INF=1LL<<60; struct edge{ int from; int to; ll cost; int id; edge(int from_,int to_,ll cost_,int id_):from(from_),to(to_),cost(cost_),id(id_){}; }; int main(){ int N,K; cin>>N>>K; vector<vector<edge>> G(N); vector<edge> edges; vector<int> A(N-1),B(N-1),C(N-1); for(int i=0;i<N-1;i++){ cin>>A[i]>>B[i]>>C[i]; A[i]--; B[i]--; G[A[i]].emplace_back(A[i],B[i],C[i],i); G[B[i]].emplace_back(B[i],A[i],C[i],i); } vector<int> cnt(N);//cnt[i]=辺iをとおっていくことができる葉の数 //部分木の葉の数を数える auto dfs=[&](auto f,int now,int pre)->int{ bool flag=true; int res=0; for(edge e:G[now]){ if(e.to==pre) continue; flag=false; int tmp=f(f,e.to,now); //e.toの部分木にある葉の数 cnt[e.id]+=tmp; res+=tmp; } if(flag){ //now自身が葉 return 1; } return res; }; dfs(dfs,0,-1); vector<pair<ll,ll>> load; for(int i=0;i<N-1;i++){ //i番目の辺 load.emplace_back(C[i],C[i]*cnt[i]); //i番目の辺の重さとコスト } vector<vector<ll>> dp(N,vector<ll>(K+1,-INF)); dp[0][0]=0; for(int i=0;i<N-1;i++){ for(int j=0;j<=K;j++){ if(dp[i][j]==-INF) continue; dp[i+1][j]=max(dp[i+1][j],dp[i][j]); if(j+load[i].first<=K){ dp[i+1][j+load[i].first]=max(dp[i+1][j+load[i].first],dp[i][j]+load[i].second); } } } ll tot=0; auto dfs2=[&](auto f,int now,int pre,ll depth)->void{ bool flag=true; int res=0; for(edge e:G[now]){ if(e.to==pre) continue; flag=false; f(f,e.to,now,depth+e.cost); } if(flag){ //nowが根の場合のみ 足される tot+=depth; } return; }; dfs2(dfs2,0,-1,0); ll add=0; for(int j=0;j<=K;j++){ add=max(add,dp[N-1][j]); } ll ans=add+tot; cout<<ans<<endl; }