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 <iostream>
#include <vector>
using namespace std;
using ll = long long;
const int MAXN = 100008;
vector<pair<int, ll>> edg[MAXN];
vector<ll> prof[MAXN];
vector<ll> merge(vector<ll>& a, vector<ll>& b) {
int n = a.size();
int m = b.size();
vector<ll> ans(n + m - 1);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
ans[i + j] = max(ans[i + j], a[i] + b[j]);
}
}
return ans;
}
vector<ll> dfs(int v, int rt) {
if (edg[v].empty() || edg[v].size() == 1 && edg[v][0].first == rt) {
return {0, 0};
}
vector<vector<ll>> prof_edg;
for (auto [u, d] : edg[v]) {
if (u == rt)
continue;
prof_edg.push_back(dfs(u, v));
for (auto& x : prof_edg.back()) {
x += d;
}
prof_edg.back()[0] -= d;
}
while (prof_edg.size() > 1) {
int n = prof_edg.size();
vector<vector<ll>> new_profedg;
for (int i = 0; i + 1 < n; i += 2) {
new_profedg.push_back(merge(prof_edg[i], prof_edg[i + 1]));
}
if (n % 2) {
new_profedg.push_back(prof_edg.back());
}
prof_edg = new_profedg;
}
return prof_edg.back();
}
signed main() {
int n, k;
cin >> n >> k;
for (int i = 0; i + 1 < n; ++i) {
int u, v, c;
cin >> u >> v >> c;
--u, --v;
edg[u].push_back({v, c});
edg[v].push_back({u, c});
}
for (int i = 0; i < n; ++i) {
vector<ll> ans = dfs(i, -1);
if (k >= ans.size())
cout << ans.back() << '\n';
else
cout << ans[k] << '\n';
}
}
Compilation message (stderr)
Main.cpp: In function 'std::vector<long long int> dfs(int, int)':
Main.cpp:23:46: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
23 | if (edg[v].empty() || edg[v].size() == 1 && edg[v][0].first == rt) {
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:62:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | if (k >= ans.size())
| ~~^~~~~~~~~~~~~
# | 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... |