QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#447311 | #8518. Roars III | marher | WA | 6ms | 4060kb | C++14 | 4.3kb | 2024-06-18 09:30:27 | 2024-06-18 09:30:27 |
Judging History
answer
#pragma GCC optimize("Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
const int N=4e5+50,inf=1e9+7;
struct edge
{
int u,v,nxt;
}e[N];
int n,head[N],cnt,dep[N],f[N],fa[N],dfn[N],cc,siz[N],p[N],pre[N],val;
char s[N];
void dfs(int x,int F)
{
fa[x]=F;dep[x]=dep[F]+1;
dfn[x]=++cc;p[cc]=x;
siz[x]=1;
for(int i=head[x];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==F)continue;
dfs(v,x);siz[x]+=siz[v];
}
}
#define ls (x<<1)
#define rs (x<<1|1)
#define mid ((l+r)>>1)
#define LS ls,l,mid
#define RS rs,mid+1,r
#define mk(x,y) make_pair(x,y)
int la[N];
pair<int,int>mx[N];
void up(int x)
{
mx[x]=max(mx[ls],mx[rs]);
}
void build(int x,int l,int r)
{
if(l==r)
{
int u=p[l];
mx[x]=mk(s[u]?dep[u]:-inf,u);
return;
}
build(LS);build(RS);up(x);
}
void ad(int x,int d)
{
la[x]+=d;mx[x].first+=d;
}
void down(int x)
{
if(la[x]==0)return;
ad(ls,la[x]);ad(rs,la[x]);
la[x]=0;
}
pair<int,int>find(int x,int l,int r,int L,int R)
{
if(L>R)return mk(-inf,0);
if(L<=l&&R>=r)return mx[x];
down(x);
pair<int,int>ans;ans.first=-inf;
if(L<=mid)ans=max(ans,find(LS,L,R));
if(R>mid)ans=max(ans,find(RS,L,R));
return ans;
}
void sol(int x,int l,int r,int d,int w)
{
if(l==r)
{
// cout<<"SOL "<<x<<' '<<l<<' '<<r<<' '<<d<<' '<<w<<' '<<(dep[p[l]]+la[x])<<' '<<p[l]<<'\n';
mx[x]=mk(w?(dep[p[l]]+la[x]):-inf,p[l]);
return;
}
down(x);
if(d<=mid)sol(LS,d,w);
else sol(RS,d,w);
up(x);
}
void add(int x,int l,int r,int L,int R,int d)
{
if(L>R)return;
if(L<=l&&R>=r)return ad(x,d);
down(x);
if(L<=mid)add(LS,L,R,d);
if(R>mid)add(RS,L,R,d);
up(x);
}
void solve(int u,int v)
{
pre[u]=v;
sol(1,1,n,dfn[v],0);
sol(1,1,n,dfn[u],1);
val+=dep[v]-dep[u];
}
void sol(int x)
{
for(int i=head[x];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa[x])continue;
sol(v);
}
if(s[x])return;
int l=dfn[x],r=l+siz[x]-1;
auto pos=find(1,1,n,l,r);
if(pos.first!=-inf)solve(x,pos.second);
}
void back(int x)
{
int u=pre[x];pre[x]=0;
sol(1,1,n,dfn[x],0);
sol(1,1,n,dfn[u],1);
val-=dep[u]-dep[x];
}
void solve(int x)
{
f[x]=val;
// cout<<x<<' '<<val<<'\n';
for(int i=head[x];i;i=e[i].nxt)if(e[i].v!=fa[x])
{
int u=pre[x],v=e[i].v,l=dfn[v],r=l+siz[v]-1,f=0,c,d;
// cout<<"Find "<<x<<' '<<v<<' '<<val<<'\n';
if(dfn[u]>=l&&dfn[u]<=r)f=1,back(x);
int t=pre[v];if(t)back(v);
// cout<<x<<' '<<v<<' '<<u<<' '<<t<<' '<<val<<'\n';
// cout<<"DDD "<<x<<' '<<v<<' '<<mx[1].first<<' '<<mx[1].second<<'\n';
add(1,1,n,1,l-1,1);
add(1,1,n,l,r,-1);
add(1,1,n,r+1,n,1);
auto[a,b]=mx[1];
// cout<<a<<' '<<b<<'\n';
if(!s[v])
{
val+=a-1;pre[v]=b;
sol(1,1,n,dfn[b],0);
sol(1,1,n,dfn[v],1);
}
if(f)
{
auto [cc,dd]=max(find(1,1,n,1,l-1),find(1,1,n,r+1,n));
c=cc;d=dd;
if(c==-inf)f=0;
else
sol(1,1,n,dfn[d],0),
sol(1,1,n,dfn[x],1),
val+=c-2;
// cout<<c<<' '<<d<<'\n';
}
// puts("");
solve(v);
if(f)
{
sol(1,1,n,dfn[d],1);
sol(1,1,n,dfn[x],0);
val-=c-2;
}
if(!s[v])
{
sol(1,1,n,dfn[b],1);
sol(1,1,n,dfn[v],0);
val-=a-1;pre[v]=0;
}
add(1,1,n,1,l-1,-1);
add(1,1,n,l,r,1);
add(1,1,n,r+1,n,-1);
if(t)solve(v,t);
if(f)solve(x,u);
// cout<<"ED "<<x<<' '<<v<<' '<<val<<' '<<c<<' '<<d<<'\n';
}
// cout<<"End "<<x<<' '<<val<<'\n';
}
main()
{
// freopen("in.txt","r",stdin);
cin>>n>>(s+1);
for(int i=1;i<=n;i++)s[i]-='0';
for(int i=1,u,v;i<n;i++)
{
cin>>u>>v;
e[++cnt]=(edge){u,v,head[u]};head[u]=cnt;
e[++cnt]=(edge){v,u,head[v]};head[v]=cnt;
}
dfs(1,0);build(1,1,n);sol(1);solve(1);
for(int i=1;i<=n;i++)cout<<f[i]<<' ';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3648kb
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: 3728kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3668kb
input:
2 10 1 2
output:
0 1
result:
ok 2 number(s): "0 1"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3644kb
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: 0ms
memory: 3748kb
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: 1ms
memory: 3752kb
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: 0
Accepted
time: 1ms
memory: 3636kb
input:
8 11001000 4 2 7 2 5 7 6 2 3 1 6 3 7 8
output:
5 3 5 5 4 4 4 6
result:
ok 8 numbers
Test #9:
score: 0
Accepted
time: 1ms
memory: 3740kb
input:
64 1110101001010110011100110000010100011011010001000111010110100101 57 60 58 63 7 43 64 9 19 8 62 17 4 40 41 18 34 56 46 14 41 36 57 26 46 58 16 32 59 1 8 17 17 13 34 29 55 10 43 5 34 8 28 36 6 10 37 21 11 48 2 8 56 55 3 8 56 61 53 52 49 51 20 30 52 39 57 22 9 49 18 16 9 27 50 52 38 40 59 43 2 18 31...
output:
34 29 29 33 34 37 34 29 31 30 49 31 41 33 27 36 34 29 29 27 30 36 30 27 34 36 38 46 29 27 38 36 50 27 33 36 30 33 30 27 36 40 34 34 31 33 38 40 36 30 36 30 37 36 30 27 29 33 34 36 32 34 40 27
result:
ok 64 numbers
Test #10:
score: 0
Accepted
time: 1ms
memory: 3692kb
input:
512 11100000100000011111111011010110001001101100011110001001111001011011000001010000100011011101001001101100010000011100000110101010100001001110000011010000111001000110010011000100000010010000100100000000100001001011100010100100101101010000111100110010001010010011101010001000111001001010111111111101...
output:
229 209 213 228 257 224 211 243 208 238 231 211 246 231 235 206 211 208 220 225 226 206 220 251 215 220 238 234 230 208 215 235 230 220 220 235 233 217 243 250 206 220 221 253 230 206 240 209 208 220 215 251 228 221 228 219 208 208 219 256 221 206 222 208 228 230 208 230 235 241 228 223 231 221 229 ...
result:
ok 512 numbers
Test #11:
score: -100
Wrong Answer
time: 6ms
memory: 4060kb
input:
4096 0011111001111111001000100000001010100111001000010100110000110111011111111001001101101010011010001010111100111110001111000000000111001110100110100011011101110011101111001010011011110101111110000101111111100101101001001000000110100111100010110110011001111111011111100111001101111110110111000010101...
output:
1579 -999998368 -999998479 -999998465 -999998456 -999998484 -999998485 -999998454 -999998454 -999998475 -999998474 -999998456 -999998445 -999998473 -999998443 -999998439 -999998424 -999998448 -999998454 -999998470 -999998474 -999998439 -999998466 -999998441 -999998444 -999998450 -999998461 -99999847...
result:
wrong answer 2nd numbers differ - expected: '1639', found: '-999998368'