QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#447312 | #8518. Roars III | marher | RE | 56ms | 18872kb | C++14 | 4.2kb | 2024-06-18 09:32:54 | 2024-06-18 09:32:55 |
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;
if(dfn[u]>=l&&dfn[u]<=r)f=1,back(x);
int t=pre[v];if(t)back(v);
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];
if(!s[v])
{
if(a<0)
{
cout<<"NOT FIND \n";
exit(0);
}
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<0)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]<<' ';
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 11848kb
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: 2ms
memory: 11920kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 15920kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 2ms
memory: 13884kb
input:
2 10 1 2
output:
0 1
result:
ok 2 number(s): "0 1"
Test #5:
score: 0
Accepted
time: 1ms
memory: 13832kb
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: 15932kb
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: 11768kb
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: 0ms
memory: 13892kb
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: 2ms
memory: 13944kb
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: 2ms
memory: 15956kb
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: 0
Accepted
time: 7ms
memory: 14032kb
input:
4096 0011111001111111001000100000001010100111001000010100110000110111011111111001001101101010011010001010111100111110001111000000000111001110100110100011011101110011101111001010011011110101111110000101111111100101101001001000000110100111100010110110011001111111011111100111001101111110110111000010101...
output:
1579 1639 1528 1542 1551 1523 1522 1553 1553 1532 1533 1551 1562 1534 1564 1568 1583 1559 1553 1537 1533 1568 1541 1566 1563 1557 1546 1537 1551 1586 1570 1548 1576 1587 1523 1541 1539 1522 1538 1565 1554 1589 1548 1614 1570 1570 1549 1527 1578 1520 1569 1581 1540 1540 1551 1579 1594 1550 1551 1550 ...
result:
ok 4096 numbers
Test #12:
score: 0
Accepted
time: 56ms
memory: 18872kb
input:
32768 010000100111010011101111111011001000100100101001100000110010100111111101110011000110001011001001100110111010010111000001110101000011101010010101111101001100100011101101010000001011001010100011011011001001111101011000001101101110101010001001011011101001010101010000011111100011110100011000001110...
output:
12964 12950 12999 13070 12980 12974 12927 12949 12952 12953 12999 12962 12972 12972 12951 12989 12948 12971 12930 12954 13012 12972 12946 12985 12946 12926 12996 13037 12915 12915 13011 13017 12939 13055 13044 12986 12986 12984 12968 12958 12960 12973 12981 12976 12992 13011 12991 12930 13001 13018 ...
result:
ok 32768 numbers
Test #13:
score: -100
Runtime Error
input:
200000 10010111111100101000010111101000010111110101100011001101010001010011111001000101111110000010001000011111011011100111001110101011010011111001010100110100100100110110100101010111011010010110111000000100100010100010111001011111011010111010111001000101010111010011010000111011001110000100010110001...