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 "roads.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = (int)1e5 +12;
vector<pair<int,int>> g[maxn];
ll dp[maxn][2];
int k;
int pos[maxn],id[maxn];
int dep[maxn];
vector<ll> pref[maxn],pref1[maxn];
int n;
int t[maxn * 4];
vector<int> P;
void build(int v = 1,int tl = 0,int tr = n - 1){
if(tl == tr) {
t[v] = (int)g[P[tl]].size();
}else {
int tm = (tl + tr) >> 1;
build(v + v,tl,tm);
build(v + v + 1,tm + 1,tr);
t[v] = max(t[v + v],t[v + v + 1]);
}
}
void upd(int pos,int val,int v = 1,int tl = 0,int tr = n - 1){
if(tl == tr) {
t[v] = val;
}else {
int tm = (tl + tr) >> 1;
if(pos <= tm) upd(pos,val,v+v,tl,tm);
else upd(pos,val,v+v+1,tm+1,tr);
t[v] = max(t[v + v],t[v + v + 1]);
}
}
int get(int v = 1,int tl = 0,int tr = n - 1){
if(t[v] <= k) return -1;
if(tl == tr){
return P[tl];
}else{
int tm = (tl + tr) >> 1;
if(t[v + v] > k) return get(v+v,tl,tm);
return get(v+v+1,tm+1,tr);
}
}
int temp[maxn],it[maxn],_j[maxn],ww[maxn];
vector<int> ii[maxn];
int vis[maxn],timer = 0,ff[maxn];
vector<pair<int,int>> bf,ch[maxn];
bool is_pr[maxn];
struct segtree{
int n;
vector<ll> c,t;
void init(int _n){
n = _n;
c.assign(_n * 4,0);
t.assign(_n * 4,0);
}
void acti(int pos,ll val,int v,int tl,int tr){
if(tl == tr){
c[v] = 1;
t[v] = val;
}else{
int tm = (tl + tr) >> 1;
if(pos <= tm) acti(pos,val,v+v,tl,tm);
else acti(pos,val,v+v+1,tm+1,tr);
c[v] = c[v + v] + c[v + v + 1];
t[v] = t[v + v] + t[v + v + 1];
}
}
ll getk(int k,int v,int tl,int tr){
if(k <= 0) return 0;
if(tl == tr) return t[v];
int tm = (tl + tr) >> 1;
if(c[v + v] >= k) return getk(k,v+v,tl,tm);
return t[v + v] + getk(k - c[v + v],v + v + 1,tm + 1,tr);
}
}tt[maxn];
int _p[maxn];
int col = 0;
void prec(int v,int pr = -1,int _w = 0) {
vector<pair<int,int>> aa;
ww[v] = _w;
tt[v].init((int)g[v].size());
_p[v] = pr;
for(auto [to,w]:g[v]) {
aa.push_back({w,to});
if(to == pr) continue;
ch[v].push_back({to,w});
dep[to] = dep[v] + 1;
prec(to,v,w);
}
sort(aa.rbegin(),aa.rend());
for(int j = 0;j < (int)aa.size();j++){
temp[aa[j].second] = j;
}
for(int j = 0;j < (int)ch[v].size();j++){
ii[v].push_back(temp[ch[v][j].first]);
}
if(pr != -1){
_j[v] = temp[pr];
assert(v);
col++;
assert(temp[pr] != -1);
}
it[v] = (int)ch[v].size() - 1;
}
void dfs(int v,int pr = -1){
vis[v] = timer;
upd(id[v],0);
bf.push_back({id[v],(int)g[v].size()});
ll S = 0;
vector<ll> can;
if(pr == -1 && _j[v] != -1){
tt[v].acti(_j[v],ww[v],1,0,tt[v].n-1);
can.push_back(-ww[v]);
}
int i = 0;
for(auto [to,w]:ch[v]){
int sz = (int)g[to].size();
if(1 || sz > k){
if(sz > k) dfs(to,v);
else dp[to][0] = dp[to][1] = 0;
S += w + dp[to][0];
can.push_back(dp[to][1] - (dp[to][0] + w));
}else{
while(it[v] >= i){
tt[v].acti(ii[v][it[v]],ch[v][it[v]].second,1,0,tt[v].n-1);
it[v]--;
}
break;
}
i++;
}
S += tt[v].t[1];
sort(can.begin(),can.end());
ll mx0 = tt[v].getk(k,1,0,tt[v].n-1),mx1 = tt[v].getk(k-1,1,0,tt[v].n-1);
ll pre = 0;
for(int i = 0;i <(int)can.size();i++){
pre -= can[i];
if(i + 1 <= k){
mx0 = max(mx0,pre + tt[v].getk(k-i-1,1,0,tt[v].n-1));
}
if(i + 1 <= k - 1){
mx1 = max(mx1,pre + tt[v].getk(k-i-2,1,0,tt[v].n-1));
}
}
dp[v][0] = dp[v][1] = S;
dp[v][0] -= mx0;
dp[v][1] -= mx1;
}
vector<long long> minimum_closure_costs(int N,vector<int> U,vector<int> V,vector<int> W) {
n = N;
P.resize(n);
for(int i = 0;i < N - 1;i++){
g[U[i]].emplace_back(V[i],W[i]);
g[V[i]].emplace_back(U[i],W[i]);
}
for(int i = 0;i < N;i++) {
sort(g[i].begin(),g[i].end(),[&](pair<int,int> x,pair<int,int> y) {
return ((int)g[x.first].size()) > (int)g[y.first].size();
});
}
vector<ll> res;
memset(_j,-1,sizeof(_j));
prec(0);
assert(col == n - 1);
iota(P.begin(),P.end(),0);
sort(P.begin(),P.end(),[&](int x,int y){
return dep[x] < dep[y];
});
build();
for(int i = 0;i < N;i++) {
id[P[i]] = i;
}
for(int i = 0;i < N;i++) {
ll ss = 0;
++timer;
k = i;
bf.clear();
while(true){
int v = get();
if(v!=-1){
dfs(v);
ss += dp[v][0];
}else break;
}
for(auto [x,y]:bf){
upd(x,y);
}
res.push_back(ss);
// cout << ss << "x\n";
}
return res;
}
# | 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... |