QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#702539 | #7736. Red Black Tree | bessie_goes_moo | WA | 148ms | 160980kb | C++17 | 2.0kb | 2024-11-02 16:10:04 | 2024-11-02 16:10:09 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
int read(){
int red=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){ if(ch=='-') f=-f;ch=getchar();}
while(ch>='0'&&ch<='9') red=red*10+ch-48,ch=getchar();
return red*f;
}
const int N=1e5+5;
int n,ans[N],dp[N],id[N],low[N],to[N],fz[N];
char s[N];
vector<int>V[N];
deque<int>f[N][2];
void dfs(int x){
low[x]=1e9;
dp[x]=(s[x]=='1');
int num=0;
for(auto v:V[x]){
dfs(v);num++;
dp[x]+=dp[v];
low[x]=min(low[x],low[v]+1);
}
if(!num){
id[x]=x;low[x]=1;ans[x]=0;
if(s[x]=='1') f[x][0].push_back(-1);
else f[x][1].push_back(1);
}else if(num==1){
int v=V[x][0];
id[x]=id[v];ans[x]=ans[v];
if(s[x]=='1') f[id[x]][0].push_back(-1);
else f[id[x]][1].push_back(1);
}else{
id[x]=x;ans[x]=dp[x];
for(int i=0;i<low[x]-1;i++){
int now=0;
for(int v:V[x]){
if(!f[id[v]][0].empty())
now+=f[id[v]][0].front(),f[id[v]][0].pop_front();
else if(fz[id[v]]) fz[id[v]]--;
else now+=f[id[v]][1].front(),f[id[v]][1].pop_front();
}
if(now<0) f[x][0].push_back(now),ans[x]+=now;
else if(now==0) fz[x]++;
else f[x][1].push_back(now);
}
if(s[x]=='1') f[x][0].push_back(-1),ans[x]--;
else f[x][1].push_back(1);
}
}
void clear(){
for(int i=1;i<=n;i++) V[i].clear();
for(int i=1;i<=n;i++){
f[i][0].clear();
f[i][1].clear();
fz[i]=0;
}
}
int main(){
#ifdef __LOCAL__
freopen("D.in","r",stdin);
freopen("D.out","w",stdout);
#endif
int t=read();
while(t--){
clear();
n=read();
scanf("%s",s+1);
for(int i=2;i<=n;i++)
V[read()].push_back(i);
dfs(1);
for(int i=1;i<n;i++) printf("%d ",ans[i]);
printf("%d\n",ans[n]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 24ms
memory: 141368kb
input:
2 9 101011110 1 1 3 3 3 6 2 2 4 1011 1 1 3
output:
4 1 2 0 0 0 0 0 0 2 0 0 0
result:
ok 2 lines
Test #2:
score: -100
Wrong Answer
time: 148ms
memory: 160980kb
input:
6107 12 000000001000 1 2 3 2 5 4 4 7 3 8 11 19 1100111101111011110 1 2 1 1 4 5 2 4 3 2 2 7 10 2 11 3 15 5 7 0111110 1 1 2 2 1 5 3 000 1 1 7 1000011 1 2 3 3 5 4 7 0001001 1 1 1 3 5 3 8 00111000 1 1 3 2 5 2 7 11 11111110111 1 1 1 4 5 4 5 2 5 1 15 110101101000010 1 2 3 2 1 5 2 5 6 5 8 7 9 14 10 0101000...
output:
1 1 1 1 0 0 0 0 0 0 0 0 6 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 2 1 0 0 0 0 0 0 4 0 0 2 1 0 0 0 0 0 0 4 3 0 0 2 0 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 6 5 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 5 1 0 1 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 6 4 ...
result:
wrong answer 14th lines differ - expected: '1 0 0 0 0 0 0 0 0 0 0 0 0', found: '2 0 0 0 0 0 0 0 0 0 0 0 0'