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;
typedef long long ll;
const ll inf = 1e14;
const int MAXN = 1e5 + 25;
vector <pair <int, int>> adj[MAXN];
ll dp[MAXN][2], cur; int n;
void dfs (int pos, int par) {
ll sum = 0;
for (auto j : adj[pos]) {
if (j.first != par) {
dfs(j.first, pos);
sum += dp[j.first][0];
}
}
dp[pos][0] = dp[pos][1] = sum;
vector <ll> k;
for (auto j : adj[pos]) {
if (j.first != par) {
k.push_back(j.second + dp[j.first][1] - dp[j.first][0]);
}
}
sort(k.begin(), k.end());
int z = (int)adj[pos].size() - (par != -1);
ll mx1 = inf, mx2 = inf;
sum = 0;
k.insert(k.begin(), 0);
int p1=max(0ll,z-cur-(par!=-1));
int p2=max(0ll,z-cur);
for (int j = 0; j < (int)k.size(); j++) {
sum += k[j];
if (j == p1) mx1 = min(mx1, sum);
if (j == p2) mx2 = min(mx2, sum);
}
dp[pos][0] += mx1; dp[pos][1] += mx2;
}
vector <ll> minimum_closure_costs (int N, vector <int> u, vector <int> v, vector <int> w) {
n = N;
for (int i = 0; i + 1 < n; i++) {
adj[u[i] + 1].push_back({v[i] + 1, w[i]});
adj[v[i] + 1].push_back({u[i] + 1, w[i]});
}
vector <ll> ans = {};
for (int i = 0; i < n; i++) {
cur = i; for (int j = 1; j <= n; j++) dp[j][0] = dp[j][1] = 0;
dfs(1, -1);
ans.push_back(dp[1][0]);
}
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... |