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;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
vector<ll> minimum_closure_costs(int n, vector<int> U, vector<int> V, vector<int> W){
vector<pii> g[n + 1];
ll tot = 0;
for (int i = 0; i < n - 1; i++){
int u = U[i] + 1, v = V[i] + 1, w = W[i];
g[u].pb({v, w});
g[v].pb({u, w});
tot += w;
}
vector<vector<ll>> dp(n + 1, vector<ll>(n));
function<void(int, int)> dfs = [&](int v, int pr){
for (auto [i, w]: g[v]){
if (i == pr) continue;
dfs(i, v);
}
for (int k = 1; k < n; k++){
dp[v][k] = 0;
vector<ll> all;
for (auto [i, w]: g[v]){
if (i == pr) continue;
dp[v][k] += dp[i][k];
all.pb(dp[i][k - 1] - dp[i][k] + w);
}
sort(all.begin(), all.end(), greater<ll>());
for (int j = 0; j < min((int) all.size(), k); j++){
if (all[j] <= 0) break;
dp[v][k] += all[j];
}
}
};
dfs(1, 0);
vector<ll> out;
for (int i = 0; i < n; i++){
out.pb(tot - dp[1][i]);
}
return out;
}
# | 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... |