結果
問題 | No.1488 Max Score of the Tree |
ユーザー |
![]() |
提出日時 | 2021-04-23 21:37:20 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
CE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 1,407 bytes |
コンパイル時間 | 30,494 ms |
コンパイル使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2025-01-20 23:40:43 |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
コンパイルメッセージ
コンパイルが30秒の制限時間を超えました
ソースコード
#include <stdio.h> #include <atcoder/all> #include <bits/stdc++.h> using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000000 void dfs(int cur,int p,vector<vector<int>> &E,vector<int> &sz){ sz[cur] = 0; rep(i,E[cur].size()){ int to = E[cur][i]; if(to==p)continue; dfs(to,cur,E,sz); sz[cur] += sz[to]; } if(sz[cur]==0)sz[cur] = 1; } int main(){ int N,K; cin>>N>>K; map<pair<int,int>,long long> mp; vector<vector<int>> E(N); rep(i,N-1){ int a,b,c; cin>>a>>b>>c; a--;b--; E[a].push_back(b); E[b].push_back(a); mp[make_pair(a,b)] = c; mp[make_pair(b,a)] = c; } vector<int> sz(N,-1); dfs(0,-1,E,sz); vector<long long> w,v; for(auto a:mp){ int x = a.first.first,y = a.first.second; if(x>y)continue; w.push_back(a.second); v.push_back((long long)a.second * min(sz[x],sz[y])); } vector<long long> dp(K+1,0LL); rep(i,w.size()){ vector<long long> ndp(K+1,0LL); rep(j,K+1){ ndp[j] = max(ndp[j],dp[j]); if(j+w[i]<=K){ ndp[j+w[i]] = max(ndp[j+w[i]],dp[j] + v[i]); } } swap(dp,ndp); } long long ans = 0LL; rep(i,K+1){ ans = max(ans,dp[i]); } for(auto a:mp){ int x = a.first.first,y = a.first.second; if(x>y)continue; long long X = a.second; ans += (long long)a.second * min(sz[x],sz[y]); } cout<<ans<<endl; return 0; }