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 MAXN 300010
#define int long long
using namespace std;
int n,m,ans;
int fa[MAXN],w[MAXN],deg[MAXN];
int val[MAXN<<1],d[MAXN<<1],rt[MAXN],tot;
int lc[MAXN<<1],rc[MAXN<<1];
int create(int v){
val[++tot]=v;d[tot]=0;
return tot;
}
int merge(int p,int q){
if(!p||!q)return p+q;
if(val[p]<val[q])swap(p,q);
rc[p]=merge(rc[p],q);
if(d[lc[p]]<d[rc[p]])swap(lc[p],rc[p]);
d[p]=d[rc[p]]+1;return p;
}
void pop(int &p){p=merge(lc[p],rc[p]);}
signed main(){
scanf("%lld%lld",&n,&m);
for(int i=2;i<=n+m;i++){
scanf("%lld%lld",&fa[i],&w[i]);
ans+=w[i];deg[fa[i]]++;
}
for(int u=n+m;u>=2;u--){
if(u<=n)while(deg[u]-->1)pop(rt[u]);
int R=val[rt[u]];pop(rt[u]);
int L=val[rt[u]];pop(rt[u]);
rt[u]=merge(rt[u],merge(create(L+w[u]),create(R+w[u])));
rt[fa[u]]=merge(rt[fa[u]],rt[u]);
}
while(deg[1]--)pop(rt[1]);
while(rt[1])ans-=val[rt[1]],pop(rt[1]);
printf("%lld",ans);
return 0;
}
Compilation message (stderr)
fireworks.cpp: In function 'int main()':
fireworks.cpp:22:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%lld%lld",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~~~~
fireworks.cpp:24:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | scanf("%lld%lld",&fa[i],&w[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |