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>
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define REP(i, l, r) for (int i = (l); i < (r); ++i)
#define FORD(i, r, l) for (int i = (r); i >= (l); --i)
#define REPD(i, r, l) for (int i = (r)-1; i >= (l); --i)
#define pb(x) push_back(x)
#define ll long long
#define pii pair<int, int>
using namespace std;
const int N = 1e5 + 5, Ns4 = 200005;
int n;
long long ans[N];
long long dpx[N], val[N];
int sz[Ns4];
long long dd[Ns4][2];
vector<pii> S[N];
void DFS(int u, int p, int max_sz) {
int now_sz = min(sz[u], max_sz), w = 0;
vector<int> Q;
long long sum = 0, pre_sum = 0;
for (auto [c, v] : S[u])
if (v != p) {
DFS(v, u, max_sz);
long long xx = dd[v][1] + c;
long long yy = dd[v][0];
sum += xx;
if (yy - xx < 0)
Q.push_back(yy - xx);
else
w++;
}
for (int i = 0; i <= min(w, 1); i++) Q.pb(0);
now_sz = min(now_sz, (int)Q.size());
pre_sum = 1e15;
random_shuffle(Q.begin(), Q.end());
if (now_sz) {
nth_element(Q.begin(), Q.begin() + now_sz - 1, Q.end());
int alta = now_sz - 1;
int x = now_sz;
for (auto X : Q) {
if (X < alta) {
x--;
pre_sum = sum;
sum += min(0, X);
}
}
for (auto X : Q) {
if (X == alta && x) {
x--;
pre_sum = sum;
sum += min(0, X);
}
}
Q.clear();
}
if (now_sz < max_sz) pre_sum = sum;
dd[u][0] = pre_sum;
dd[u][1] = sum;
}
void calc(int i) {
DFS(0, 0, i);
ans[i] = dd[0][1];
}
void dnc(int l = 1, int r = n - 2, ll L = ans[n - 1], ll R = ans[0]) {
if (l > r) return;
if (L == R) {
for (int i = l; i <= r; i++) ans[i] = L;
return;
}
int tm = (l + r) / 2;
calc(tm);
dnc(l, tm - 1, ans[tm], R);
dnc(tm + 1, r, L, ans[tm]);
}
void sub34() {
REP(i, 0, n) {
sz[i] = S[i].size() - 1;
if (i == 0) ++sz[i];
}
calc(0);
calc(n - 1);
if (1 <= n - 2) dnc();
}
vector<long long> minimum_closure_costs(int N, vector<int> U, vector<int> V,
vector<int> W) {
n = N;
REP(i, 0, n - 1) {
S[U[i]].emplace_back(W[i], V[i]);
S[V[i]].emplace_back(W[i], U[i]);
}
sub34();
vector<long long> ret(n);
REP(i, 0, n) ret[i] = ans[i];
return ret;
}
Compilation message (stderr)
roads.cpp: In function 'void DFS(int, int, int)':
roads.cpp:22:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
22 | for (auto [c, v] : S[u])
| ^
# | 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... |