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>
#include "roads.h"
using namespace std;
using ll = long long;
#define ff first
#define ss second
#define pii pair<int, int>
vector<vector<ll>> dp;
vector<vector<pii>> E;
void dfs(int x, int k, bool f, int p = -1) {
if (dp[x][f] == LLONG_MAX) dp[x][f] = 0;
else return;
vector<ll> v;
for (auto [i, w] : E[x]) {
if (i == p) {
if (f == 1) dp[x][f] += w;
continue;
}
dfs(i, k, 0, x);
dfs(i, k, 1, x);
dp[x][f] += dp[i][0];
v.push_back(dp[i][1] - dp[i][0]);
}
sort(v.begin(), v.end());
for (int i = 0; i < (int)E[x].size() - k - f || (i < (int)v.size() && v[i] < 0); i++) {
dp[x][f] += v[i];
}
// cout << "x " << x << ' ' << f << ' ' << k << ' ' << dp[x][f] << endl;
}
vector<ll> minimum_closure_costs(int n, vector<int> U, vector<int> V, vector<int> W) {
E.assign(n, {});
vector<ll> ans = {0};
for (int i = 0; i < n - 1; i++) {
ans[0] += W[i];
E[U[i]].push_back({V[i], W[i]});
E[V[i]].push_back({U[i], W[i]});
}
for (int i = 1; i < n; i++) {
dp.assign(n, vector<ll>(2, LLONG_MAX));
dfs(0, i, 0);
ans.push_back(dp[0][0]);
// cout << endl;
}
return ans;
}
# | 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... |