Submission #537592

#TimeUsernameProblemLanguageResultExecution timeMemory
537592joelauPaths (RMI21_paths)C++14
19 / 100
1095 ms18436 KiB
#include <bits/stdc++.h> using namespace std; long long N,K, dp[2005][2005], num[2005], tmp[2005]; vector< pair<long long,long long> > lst[100005]; void dfs (long long x, long long p) { memset(dp[x],-1,sizeof(dp[x])); num[x] = 0, dp[x][0] = 0; for (auto v: lst[x]) if (v.first != p) { dfs(v.first,x); num[x] += num[v.first]; } if (num[x] == 0) num[x] = 1, dp[x][1] = 0; for (auto v: lst[x]) if (v.first != p) { for (long long i = 0; i <= num[x] && i <= K; ++i) tmp[i] = dp[x][i]; for (long long i = 1; i <= num[v.first] && i <= K && dp[v.first][i] != -1; ++i) for (long long j = 0; i+j <= num[x] && dp[x][j] != -1 && i+j <= K; ++j) tmp[i+j] = max(tmp[i+j],dp[v.first][i] + dp[x][j] + v.second); swap(tmp,dp[x]); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> N >> K; for (long long i = 0; i < N-1; ++i) { long long u,v,w; cin >> u >> v >> w; u--, v--; lst[u].emplace_back(v,w), lst[v].emplace_back(u,w); } for (long long i = 0; i < N; ++i) { dfs(i,-1); cout << dp[i][K] << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...