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>
//#include "bits_stdc++.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i)
#define RFOR(i, x, n) for (ll i =x; i>=n; --i)
using namespace std;
mt19937_64 rnd(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<pi, pi> piii;
struct balancedheap
{
multiset<ll> chosen, store;
ll k, sum;
balancedheap() {k =-1; sum = 0;}
void balance()
{
if (k==-1) return;
while (chosen.size() > k && chosen.size())
{
sum -= *prev(chosen.end());
store.insert(*prev(chosen.end()));
chosen.erase(prev(chosen.end()));
}
while (chosen.size() < k && store.size() && *store.begin() < 0)
{
sum += *store.begin();
chosen.insert(*store.begin());
store.erase(store.begin());
}
while (chosen.size() && store.size() && *prev(chosen.end()) > *store.begin())
{
ll x = *store.begin(), y = *prev(chosen.end());
sum += x - y;
store.erase(store.begin()); chosen.erase(--chosen.end());
store.insert(y); chosen.insert(x);
}
}
void insert(ll x)
{
store.insert(x);
balance();
}
void erase(ll x)
{
if (chosen.find(x)==chosen.end()) {store.erase(store.lb(x));}
else{sum -= x; chosen.erase(chosen.lb(x));}
balance();
}
void setcap(ll x){k = x; balance();}
};
ll const MAXN = 100005;
ll n, k;
set<pi> adj[MAXN];
ll vis[MAXN], dp[MAXN][2], sum[MAXN], deg[MAXN];
balancedheap hp[MAXN];
vector<pi> order;
vector<ll> ans;
void dfs(ll x, ll p)
{
vis[x] = 1;
ll ans = sum[x];
vector<ll> bt;
for (pi u: adj[x])
{
if (u.f==p) continue;
dfs(u.f, x);
ans += dp[u.f][1] + u.s;
ll ch = dp[u.f][0] - dp[u.f][1] - u.s;
hp[x].insert(ch);
bt.pb(ch);
}
hp[x].setcap(k-1);
dp[x][0] = ans +hp[x].sum;
hp[x].setcap(k);
dp[x][1] = ans + hp[x].sum;
for (ll u: bt) hp[x].erase(u);
}
std::vector<long long> minimum_closure_costs(int N, std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
n = N;
for (ll i=0; i<n; ++i) {sum[i] = deg[i] = 0;}
for (ll i=0; i<n-1; ++i)
{
adj[U[i]].insert(mp(V[i], W[i]));
adj[V[i]].insert(mp(U[i], W[i]));
deg[U[i]]++; deg[V[i]]++;
}
for (ll i=0; i<n; ++i) {order.pb(mp(deg[i], i)); vis[i] = 0;}
sort(all(order));
ll idx = 0;
ans.assign(n, 0);
//show(1);
for (ll j=0; j<n; ++j)
{
k = j;
ans[j] = 0;
while (idx < n && order[idx].f <= k)
{
ll now = order[idx].s;
for (pi u: adj[now])
{
sum[u.f] += u.s;
hp[u.f].insert(-u.s);
adj[u.f].erase(mp(now, u.s));
}
idx++;
}
for (ll i=idx; i<n; ++i) vis[order[i].s]= 0;
for (ll i=idx; i<n; ++i) if (!vis[order[i].s])
{
dfs(order[i].s, -1);
ans[j] += dp[order[i].s][1];
}
}
return ans;
}
Compilation message (stderr)
roads.cpp: In member function 'void balancedheap::balance()':
roads.cpp:45:24: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
45 | while (chosen.size() > k && chosen.size())
| ~~~~~~~~~~~~~~^~~
roads.cpp:51:24: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
51 | while (chosen.size() < k && store.size() && *store.begin() < 0)
| ~~~~~~~~~~~~~~^~~
# | 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... |