結果
問題 | No.1488 Max Score of the Tree |
ユーザー |
👑 ![]() |
提出日時 | 2021-04-23 22:01:00 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 14 ms / 2,000 ms |
コード長 | 1,033 bytes |
コンパイル時間 | 1,137 ms |
コンパイル使用メモリ | 86,176 KB |
最終ジャッジ日時 | 2025-01-20 23:52:29 |
ジャッジサーバーID (参考情報) |
judge3 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:18:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 18 | scanf("%d%d",&N,&K); | ~~~~~^~~~~~~~~~~~~~ main.cpp:21:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 21 | int u,v,d; scanf("%d%d%d",&u,&v,&d); u--; v--; | ~~~~~^~~~~~~~~~~~~~~~~~~
ソースコード
#include <iostream>#include <vector>#include <queue>using namespace std;using ll = long long;#define rep(i,n) for(int i=0; i<(n); i++)struct Edge{ int to, d; };int N, K;vector<vector<Edge>> E;vector<int> I;vector<int> P;vector<int> D;vector<int> Z;int main(){scanf("%d%d",&N,&K);E.resize(N);rep(i,N-1){int u,v,d; scanf("%d%d%d",&u,&v,&d); u--; v--;E[u].push_back({v,d});E[v].push_back({u,d});}P.assign(N,-1);D.assign(N,0);Z.assign(N,1);queue<int> Q;Q.push(0);while(Q.size()){int p = Q.front(); Q.pop();I.push_back(p);for(Edge e : E[p]) if(P[p] != e.to){P[e.to] = p;D[e.to] = e.d;Q.push(e.to);Z[p] = 0;}}for(int i=N-1; i>=1; i--){ int p=I[i]; Z[P[p]]+=Z[p]; }ll ans = 0;rep(i,N) ans += (ll)Z[i] * D[i];vector<ll> dp(K+1,0);rep(i,N){vector<ll> tmp = dp;for(int k=D[i]; k<=K; k++) tmp[k] = max(tmp[k],dp[k-D[i]]+Z[i]*D[i]);dp = move(tmp);}printf("%lld\n",ans+dp[K]);return 0;}