QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#446119 | #8518. Roars III | grass8cow | WA | 3ms | 20816kb | C++17 | 3.1kb | 2024-06-16 21:45:31 | 2024-06-16 21:45:32 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=200010;
int n;
char c[N];
vector<int>g[N];
#define pb push_back
int md[N];
int fa[N],lg[N],st[N][20],sz[N],d[N],dfn[N];
int cz(int x,int y){return (d[x]<d[y])?x:y;}
int lca(int u,int v){
if(u==v)return u;if(dfn[u]>dfn[v])swap(u,v);
int l=dfn[u]+1,r=dfn[v],k=lg[r-l+1];
return fa[cz(st[l][k],st[r-(1<<k)+1][k])];
}
int dis(int u,int v){return d[u]+d[v]-d[lca(u,v)]*2;}
#define pi pair<int,int>
#define fi first
#define se second
pi operator + (const pi &a,const pi &b){
if(!a.fi)return b;if(!b.fi)return a;
int V[4]={a.fi,a.se,b.fi,b.se},mx=-1;
pi z={0,0};
for(int i=0;i<4;i++)for(int j=i+1;j<4;j++){
int q=dis(V[i],V[j]);
if(mx<q)mx=q,z={V[i],V[j]};
}
return z;
}
#define mp make_pair
int bel[N];
void dfs(int x){
bel[dfn[x]=++dfn[0]]=x,sz[x]=1,st[dfn[x]][0]=x;
for(int v:g[x])if(v!=fa[x])fa[v]=x,d[v]=d[x]+1,dfs(v),sz[x]+=sz[v];
}
pi e[N<<2];
void build(int p,int l,int r){
if(l==r){
if(c[bel[l]]=='1')e[p]={bel[l],bel[l]};
else e[p]={0,0};return;
}
int mi=(l+r)>>1;build(p<<1,l,mi),build(p<<1|1,mi+1,r);
e[p]=e[p<<1]+e[p<<1|1];
}
pi gr(int x,pi y){
if(y.fi==0)return {-1,0};
return max(make_pair(dis(y.fi,x),y.fi),make_pair(dis(y.se,x),y.se));
}
int X;pi ox;
void ask(int p,int l,int r,int x,int y){
if(x<=l&&r<=y){ox=max(ox,gr(X,e[p]));return;}
int mid=(l+r)>>1;
if(x<=mid)ask(p<<1,l,mid,x,y);
if(y>mid)ask(p<<1|1,mid+1,r,x,y);
}
void up(int p,int l,int r,int x,int z){
if(l==r){e[p]=(z?mp(bel[l],bel[l]):mp(0,0));return;}
int mid=(l+r)>>1;
if(x<=mid)up(p<<1,l,mid,x,z);else up(p<<1|1,mid+1,r,x,z);
e[p]=e[p<<1]+e[p<<1|1];
}
ll ap[N],ans[N],ax[N];int me[N];
void dfs2(int x){
for(int v:g[x])if(v!=fa[x])dfs2(v),ap[x]+=ap[v];
ans[x]=ax[x]=ap[x];if(c[x]=='1')return;
X=x,ox={-1,0},ask(1,1,n,dfn[x],dfn[x]+sz[x]-1);
if(ox.fi==-1)return;
ap[x]+=ox.fi,me[x]=ox.se;
up(1,1,n,dfn[ox.se],0),up(1,1,n,dfn[x],1);
}
void dfs3(int x,ll dc){
if(me[x])up(1,1,n,dfn[x],0),up(1,1,n,dfn[me[x]],1);
for(int v:g[x])if(v!=fa[x]){
int jx=0;
ll de=dc+ax[x]-ap[v];
if(c[x]=='0'){
X=x,ox={-1,0};
if(dfn[v]>1)ask(1,1,n,1,dfn[v]-1);
if(dfn[v]+sz[v]-1<n)ask(1,1,n,dfn[v]+sz[v],n);
if(ox.fi!=-1)jx=ox.se;
de+=ox.fi,up(1,1,n,dfn[jx],0),up(1,1,n,dfn[x],1);
}
ans[v]+=de;
if(c[v]=='0'){ox=gr(v,e[1]);if(ox.fi!=-1)ans[v]+=ox.fi;}
dfs3(v,de);
if(jx)up(1,1,n,dfn[jx],1),up(1,1,n,dfn[x],0);
}
if(me[x])up(1,1,n,dfn[x],1),up(1,1,n,dfn[me[x]],0);
}
int main(){
scanf("%d%s",&n,c+1);
lg[0]=-1;
for(int i=1;i<=n;i++)lg[i]=lg[i>>1]+1;
for(int i=1,u,v;i<n;i++)scanf("%d%d",&u,&v),g[u].pb(v),g[v].pb(u);
dfs(1);
for(int j=1;j<20;j++)for(int i=1;i+(1<<j)-1<=n;i++)st[i][j]=cz(st[i][j-1],st[i+(1<<(j-1))][j-1]);
build(1,1,n);dfs2(1);
ans[1]=ap[1];dfs3(1,0);
for(int i=1;i<=n;i++)printf("%lld ",ans[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 18172kb
input:
5 10101 1 2 2 3 2 4 4 5
output:
2 2 2 3 3
result:
ok 5 number(s): "2 2 2 3 3"
Test #2:
score: 0
Accepted
time: 0ms
memory: 18264kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 2ms
memory: 19160kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 0ms
memory: 18148kb
input:
2 10 1 2
output:
0 1
result:
ok 2 number(s): "0 1"
Test #5:
score: 0
Accepted
time: 2ms
memory: 16852kb
input:
3 100 2 3 2 1
output:
0 1 2
result:
ok 3 number(s): "0 1 2"
Test #6:
score: 0
Accepted
time: 2ms
memory: 20816kb
input:
4 1100 4 1 4 3 4 2
output:
1 1 3 1
result:
ok 4 number(s): "1 1 3 1"
Test #7:
score: 0
Accepted
time: 2ms
memory: 19232kb
input:
5 10100 4 5 1 3 2 1 3 5
output:
0 2 0 4 2
result:
ok 5 number(s): "0 2 0 4 2"
Test #8:
score: -100
Wrong Answer
time: 0ms
memory: 19424kb
input:
8 11001000 4 2 7 2 5 7 6 2 3 1 6 3 7 8
output:
5 3 4 5 4 3 4 6
result:
wrong answer 3rd numbers differ - expected: '5', found: '4'