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>
#ifndef LOCAL
#include "roads.h"
#endif
#define ld long double
#define ll long long
#define pb push_back
using namespace std;
const int N = 1e5 + 10;
const ll INF = 1e18;
vector<pair<int,int>> g[N];
ll e[N], dp[N][2];
int k, n;
void dfs(int v, int p) {
vector<int> arr;
ll sum = 0;
for (auto to : g[v]) {
if (to.first != p) {
e[to.first] = to.second;
dfs(to.first, v);
arr.pb(to.first);
sum += dp[to.first][0];
}
}
sort(arr.begin(), arr.end(), [&](int u, int v){return dp[u][1] + e[u] - dp[u][0] > dp[v][1] + e[v] - dp[v][0];});
if (arr.size() < k) {
for (auto to : arr) {
sum += dp[to][1] + e[to] - dp[to][0];
}
dp[v][0] = dp[v][1] = sum;
} else {
if (k == 0) {
dp[v][0] = sum;
} else {
for (int i = 1; i <= k; i++) {
int to = arr[i - 1];
sum += dp[to][1] + e[to] - dp[to][0];
if (i < k) {
dp[v][1] = max(dp[v][1], sum);
} else {
dp[v][0] = sum;
}
}
dp[v][0] = max(dp[v][1], dp[v][0]);
}
}
}
vector<long long> minimum_closure_costs(int N, vector<int> u, vector<int> v, vector<int> w) {
n = N;
ll sum = 0;
for (int i = 0; i < n - 1; i++) {
g[u[i]].pb({v[i], w[i]});
g[v[i]].pb({u[i], w[i]});
sum += w[i];
}
vector<ll> ans;
for (k = 0; k < n; k++) {
for (int i = 0; i < n; i++) {
dp[i][0] = dp[i][1] = -INF;
}
dfs(0, -1);
ans.pb(sum - dp[0][0]);
}
return ans;
}
#ifdef LOCAL
int main() {
int N;
assert(1 == scanf("%d", &N));
std::vector<int> U(N - 1), V(N - 1), W(N - 1);
for (int i = 0; i < N - 1; ++i) {
assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
}
std::vector<long long> closure_costs = minimum_closure_costs(N, U, V, W);
for (int i = 0; i < static_cast<int>(closure_costs.size()); ++i) {
if (i > 0) {
printf(" ");
}
printf("%lld",closure_costs[i]);
}
printf("\n");
return 0;
}
#endif
Compilation message (stderr)
roads.cpp: In function 'void dfs(int, int)':
roads.cpp:30:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
30 | if (arr.size() < k) {
| ~~~~~~~~~~~^~~
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |