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 PB push_back
#define ST first
#define ND second
//#pragma GCC optimize ("O3")
//#pragma GCC target("tune=native")
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using vi = vector<int>;
const int nax = 100 * 1000 + 10;
int n, k;
set<pi>G[nax];
ll dp[nax][2];
vi ord[nax];
bool vis[nax];
int cnt[nax];
void dfs(int x) {
ll sum = 0;
vis[x] = true;
vector<ll>dx = {};
for(auto [nbh, w] : G[x]) if(!vis[nbh]) {
dfs(nbh);
sum += dp[nbh][0];
if(-dp[nbh][0] + dp[nbh][1] + w > 0) dx.PB(-dp[nbh][0] + dp[nbh][1] + w);
}
sum -= cnt[x];
sort(dx.rbegin(), dx.rend());
dp[x][0] = dp[x][1] = sum;
for(int i = 0; i < min(k - 1, (int)dx.size()); ++i) {
dp[x][1] += dx[i];
dp[x][0] += dx[i];
}
dp[x][1] += min(cnt[x], (k - 1) - (int)dx.size());
dp[x][0] += min(cnt[x], (k - 1) - (int)dx.size());
if((int)dx.size() >= k) {
dp[x][0] += dx[k - 1];
} else if(cnt[x] > (k - 1) - (int)dx.size()) {
dp[x][0] += 1;
}
}
vector<ll> minimum_closure_costs(int N, vi U, vi V, vi W) {
n = N;
ll sum = 0;
for(int i = 0; i < n - 1; ++i) {
G[U[i]].insert({V[i], W[i]});
G[V[i]].insert({U[i], W[i]});
sum += W[i];
}
set<int>act = {};
for(int i = 0; i < n; ++i) {
act.insert(i);
}
for(int i = 0; i < n; ++i) {
ord[(int)G[i].size()].PB(i);
}
vector<ll>res(n);
res[0] = sum;
ll delta = 0;
for(k = 1; k < n; ++k) {
for(auto v : ord[k]) {
act.erase(v);
for(auto [nbh, w] : G[v]) {
if(act.count(nbh)) {
delta += w;
G[nbh].erase({v, w});
cnt[nbh]++;
}
}
}
ll s = 0;
for(auto v : act) {
if(!vis[v]) {
dfs(v);
s += dp[v][0];
}
}
for(auto v : act)
vis[v] = false;
res[k] = (sum - (s + delta));
}
return res;
}
//int main() {
//ios_base::sync_with_stdio(0);
//cin.tie(0);
//auto vec = minimum_closure_costs(4, {0, 2, 0}, {1, 0, 3}, {5, 10, 5});
//auto vec1 = minimum_closure_costs(5, {0, 0, 0, 2}, {1, 2, 3, 4}, {1, 4, 3, 2});
//cin >> n;
//vi u(n-1), v(n-1), w(n-1);
//for(int i = 0; i < n-1; ++i) cin >> u[i] >> v[i] >> w[i];
//auto vec = minimum_closure_costs(n, u, v, w);
//for(auto x : vec) cout << x << " ";
//}
# | 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... |