This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
long long N,K, dp[1005][105], num[1005];
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) {
long long tmp[105];
for (long long i = 0; i <= num[x]; ++i) tmp[i] = dp[x][i];
for (long long i = 1; i <= num[v.first] && dp[v.first][i] != -1; ++i)
for (long long j = 0; i+j <= num[x] && dp[x][j] != -1; ++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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |