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 fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e5+10;
const ll INF=1e14;
struct item{
item *l,*r;
int val,il,pod,prior;
ll sum;
};
pitem tr[N];
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int oj[N],fau[N],kk[N],dep[N],kr[N];
ll dp[N][2];
vector<pair<int,int> >graf[N];
vector<pair<int,int> >graf2[N];
vi aa[N];
vl pom;
bitset<N>bb,vis;
int Find(int a){
if(fau[a]==a) return a;
return fau[a]=Find(fau[a]);
}
void Union(int a,int b){
a=Find(a),b=Find(b);
if(dep[kk[a]]<dep[kk[b]]) swap(a,b);
fau[a]=b;
}
void comb(pitem v){
v->sum=(v->l!=nullptr?v->l->sum:0)+(v->r!=nullptr?v->r->sum:0)+(ll)(v->val)*(ll)(v->il);
v->pod=(v->l!=nullptr?v->l->pod:0)+(v->r!=nullptr?v->r->pod:0)+(v->il);
}
pair<pitem,pitem> split(pitem v,int x){
if(!v) return {nullptr,nullptr};
if((v->val)<=x){
pair<pitem,pitem>curr=split(v->r,x);
v->r=curr.fi;
comb(v);
return {v,curr.se};
}else{
pair<pitem,pitem>curr=split(v->l,x);
v->l=curr.se;
comb(v);
return {curr.fi,v};
}
}
pitem merge(pitem v1,pitem v2){
if(!v1 or !v2) return (!v1?v2:v1);
if((v1->prior)>(v2->prior)){
v1->r=merge(v1->r,v2);
comb(v1);
return v1;
}else{
v2->l=merge(v1,v2->l);
comb(v2);
return v2;
}
}
pitem add(pitem v,int x){
pair<pitem,pitem>curr1=split(v,x);
pair<pitem,pitem>curr2=split(curr1.fi,x-1);
if(!curr2.se){
pitem nn=new item;
nn->val=x,nn->pod=1,nn->prior=uniform_int_distribution<int>(1,1000*1000*1000)(rng);
nn->sum=x,nn->il=1,nn->l=nullptr,nn->r=nullptr;
v=merge(merge(curr2.fi,nn),curr1.se);
return v;
}
curr2.se->pod++,curr2.se->il++,curr2.se->sum+=x;
v=merge(merge(curr2.fi,curr2.se),curr1.se);
return v;
}
pitem del(pitem v,int x){
pair<pitem,pitem>curr1=split(v,x);
pair<pitem,pitem>curr2=split(curr1.fi,x-1);
if(curr2.se!=nullptr){
curr2.se->il--;
curr2.se->pod--,curr2.se->sum-=x,curr2.se;
}
if(!curr2.se or curr2.se->il==0){
if(curr2.se!=nullptr) delete curr2.se;
v=merge(curr2.fi,curr1.se);
return v;
}
curr1.fi=merge(curr2.fi,curr2.se);
v=merge(curr1.fi,curr1.se);
return v;
}
ll sum(pitem v,int x){
if(!v or x==0) return 0;
ll suml=(v->sum)-(!(v->r)?0:v->r->sum);
int pp=(v->pod)-(!(v->r)?0:v->r->pod);
if(pp<=x) return sum(v->r,x-pp)+suml;
else{
int wz=max(0,x-(!(v->l)?0:v->l->pod));
return sum(v->l,x-wz)+(ll)(v->val)*(ll)wz;
}
}
void dfsi(int v,int o){
oj[v]=o;
dep[v]=dep[o]+1;
for(auto [u,c]:graf[v]){
if(u==o) continue;
tr[v]=add(tr[v],c);
kr[u]=c;
dfsi(u,v);
}
}
ll ob(vl pom,int v,int usu){
ll res=0,res2=INF;
int g=0;
for(int i=0;i<pom.size();i++){
if(pom[i]>0) break;
res+=pom[i],usu--;
g++;
}
if(usu>0){
if((!tr[v]?0:(tr[v]->pod))>=usu) res2=sum(tr[v],usu);
ll curr=0;
for(int i=g;i<pom.size();i++){
curr+=pom[i];
usu--;
usu=max(0,usu);
if((!tr[v]?0:(tr[v]->pod))>=usu) res2=min(res2,curr+sum(tr[v],usu));
}
return res+res2;
}else return res;
}
void dfs(int v,int o,int x){
pom.clear();
ll res=0;
for(auto [u,c]:graf2[v]){
if(o==u) continue;
dfs(u,v,x);
if(dp[u][0]!=INF){
pom.push_back(dp[u][1]-dp[u][0]);
res+=dp[u][0];
}else{
res+=dp[u][1];
x++;
}
}
sort(pom.begin(),pom.end());
dp[v][0]=ob(pom,v,graf[v].size()-x)+res;
dp[v][1]=ob(pom,v,graf[v].size()-(x+1))+res+kr[v];
dp[v][0]=min(dp[v][0],INF),dp[v][1]=min(dp[v][1],INF);
}
vl minimum_closure_costs(int n,vi u,vi v,vi w){
ll xd=0;
for(int i=0;i<n-1;i++){
u[i]++,v[i]++;
graf[u[i]].push_back({v[i],w[i]}),graf[v[i]].push_back({u[i],w[i]});
}
for(int i=1;i<=n;i++) fau[i]=i,kk[i]=i;
dfsi(1,1);
kr[1]=1e9;
for(int i=1;i<=n;i++) aa[graf[i].size()].push_back(i);
/*for(int i=1;i<=n;i++){
for(auto [u,c]:graf[i]){
graf2[i].push_back({u,c});
if(oj[i]!=u) tr[i]=del(tr[i],c);
}
}*/
vl res(n);
vi k;
for(int i=3;i>=0;i--){
for(auto v:aa[i]){
for(auto [u,c]:graf[v]){
if(bb[u]){
Union(v,u);
graf2[u].push_back({v,c}),graf2[v].push_back({u,c});
if(oj[v]!=u) tr[v]=del(tr[v],c);
if(oj[u]!=v) tr[u]=del(tr[u],c);
}
}
k.push_back(v);
bb[v]=1;
}
ll ans=0;
for(auto u:k){
if(vis[Find(u)]) continue;
vis[Find(u)]=1;
dfs(kk[Find(u)],kk[Find(u)],i);
ans+=min(dp[kk[Find(u)]][0],dp[kk[Find(u)]][1]);
}
for(auto u:k) vis[Find(u)]=0;
res[i]=ans;
}
return res;
}
Compilation message (stderr)
roads.cpp: In function 'item* del(item*, int)':
roads.cpp:3:12: warning: right operand of comma operator has no effect [-Wunused-value]
3 | #define se second
| ^
roads.cpp:83:48: note: in expansion of macro 'se'
83 | curr2.se->pod--,curr2.se->sum-=x,curr2.se;
| ^~
roads.cpp: In function 'void dfsi(int, int)':
roads.cpp:107:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
107 | for(auto [u,c]:graf[v]){
| ^
roads.cpp: In function 'll ob(vl, int, int)':
roads.cpp:117:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
117 | for(int i=0;i<pom.size();i++){
| ~^~~~~~~~~~~
roads.cpp:125:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | for(int i=g;i<pom.size();i++){
| ~^~~~~~~~~~~
roads.cpp: In function 'void dfs(int, int, int)':
roads.cpp:138:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
138 | for(auto [u,c]:graf2[v]){
| ^
roads.cpp: In function 'vl minimum_closure_costs(int, vi, vi, vi)':
roads.cpp:174:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
174 | for(auto [u,c]:graf[v]){
| ^
roads.cpp:155:8: warning: unused variable 'xd' [-Wunused-variable]
155 | ll xd=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... |