This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #pragma GCC target ("avx,avx2,fma")
// #pragma GCC optimize ("Ofast,inline") // O1 - O2 - O3 - Os - Ofast
// #pragma GCC optimize ("unroll-loops")
#include "roads.h"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define per(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) x.size()
#define pb push_back
#define debug(x) cout << #x << " = " << x << endl
#define umap unordered_map
#define uset unordered_set
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const int INF = 1'000'000'007;
int n, k, vis[100001],deg[100001];
set<ii> graph[100001];
ll memo[100001][2], sums[100001];
vii degree;
multiset<ll>pqs[100001];
void dp(int u, int p) {
vis[u]=k;
ll ans = sums[u];
vll diff;
trav(edge, graph[u]) if (edge.first != p) {
int v, w;
tie(v, w) = edge;
dp(v, u);
ans += memo[v][0];
diff.emplace_back((memo[v][1] + w) - memo[v][0]);
}
sort(all(diff));
diff.emplace_back(1e17);
memo[u][0] = memo[u][1] = 1e16;
int need_rem = max(0,deg[u] - k - (int)sz(pqs[u]));
vll removed,added;
rep(i,0,min(int(sz(diff)-1),need_rem)) {
ll val=diff[i];
added.pb(val);
pqs[u].insert(val);
ans+=val;
}
if(sz(added)<need_rem) memo[u][1]=ans;
else {
//debug(u);
rep(i,need_rem,sz(diff)) {
memo[u][0]=min(memo[u][0],ans);
memo[u][1]=min(memo[u][1],ans - *(--pqs[u].end()));
ll val=diff[i];
if(val<*(--pqs[u].end())) {
added.pb(val);
ans+=val;
auto it=(--pqs[u].end());
removed.pb(*it);
ans-=*it;
pqs[u].erase(it);
pqs[u].insert(val);
} else if(val<0) ans+=val;
else break;
}
}
trav(val,added) pqs[u].erase(val);
trav(val,removed)pqs[u].insert(val);
//cout<<"u = "<<u+1<<" need_rem = "<<need_rem<<' '<<memo[u][0]<<' '<<memo[u][1]<<endl;
}
vector<long long> minimum_closure_costs(int N, vector<int> U, vector<int> V, vector<int> W) {
n = N;
memset(vis, -1, sizeof(vis));
memset(deg, 0, sizeof(deg));
memset(sums, 0, sizeof(sums));
degree.assign(n, {0, 0});
vll ans(n, 0);
rep(i, 0, n - 1) {
graph[U[i]].emplace(V[i], W[i]);
graph[V[i]].emplace(U[i], W[i]);
++degree[U[i]].first;
degree[U[i]].second = U[i];
++degree[V[i]].first;
degree[V[i]].second = V[i];
++deg[U[i]];
++deg[V[i]];
}
sort(all(degree));
int removed = 0;
rep(i, 0, n) {
k = i;
while (removed<n&°ree[removed].first <= k) {
int u = degree[removed].second;
trav(edge, graph[u]) {
int v,w;
tie(v,w)=edge;
graph[v].erase({u,w});
sums[v]+=w;
pqs[v].insert(w);
}
//debug(u+1);
++removed;
}
rep(j,removed,n) {
int u=degree[j].second;
while(sz(pqs[u])>deg[u]-k){
auto it=(--pqs[u].end());
sums[u]-=*it;
pqs[u].erase(it);
}
}
rep(j, removed, n) if (vis[degree[j].second] != k) {
int u=degree[j].second;
dp(u, u);
ans[k] += memo[u][0];
}
}
return ans;
}
/*
int main() {
int N;
assert(1 == scanf("%d", &N));
vector<int> U(N - 1), V(N - 1), W(N - 1);
for (int i = 0; i < N - 1; ++i) {
assert(3 == scanf("%d %d %d", &U[i], &V[i], &W[i]));
}
vector<long long> closure_costs = minimum_closure_costs(N, U, V, W);
for (int i = 0; i < static_cast<int>(closure_costs.size()); ++i) {
if (i > 0) {
printf(" ");
}
printf("%lld", closure_costs[i]);
}
printf("\n");
return 0;
}*/
Compilation message (stderr)
roads.cpp: In function 'void dp(int, int)':
roads.cpp:68:17: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
68 | if(sz(added)<need_rem) memo[u][1]=ans;
| ^
roads.cpp:11:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | #define rep(i, a, b) for (int i = (a); i < (b); ++i)
| ^
roads.cpp:71:9: note: in expansion of macro 'rep'
71 | rep(i,need_rem,sz(diff)) {
| ^~~
roads.cpp: In function 'std::vector<long long int> minimum_closure_costs(int, std::vector<int>, std::vector<int>, std::vector<int>)':
roads.cpp:136:29: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
136 | while(sz(pqs[u])>deg[u]-k){
| ^
# | 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... |