QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#702873#7736. Red Black Treebessie_goes_mooWA 159ms162676kbC++172.0kb2024-11-02 16:43:272024-11-02 16:43:27

Judging History

你现在查看的是最新测评结果

  • [2024-11-02 16:43:27]
  • 评测
  • 测评结果:WA
  • 用时:159ms
  • 内存:162676kb
  • [2024-11-02 16:43:27]
  • 提交

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=1;i<low[x];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();
            }
            // printf("%d %d\n",x,now);
            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_front(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;
}

详细

Test #1:

score: 100
Accepted
time: 24ms
memory: 141456kb

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: 159ms
memory: 162676kb

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
1 0 0 0 0 0 0 0 0 0 0 0 0
5 3 ...

result:

wrong answer 4318th lines differ - expected: '2 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0', found: '3 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'