QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#680634#7736. Red Black TreeYoralenTL 1ms9900kbC++141.2kb2024-10-26 21:54:072024-10-26 21:54:07

Judging History

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

  • [2024-10-26 21:54:07]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:9900kb
  • [2024-10-26 21:54:07]
  • 提交

answer

#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
const int N=200005;
struct edge{int to,next;}w[N<<1];
int n,h[N],cnt,T,a[N];
char s[N];
void Add(int x,int y){
    w[++cnt].to=y,w[cnt].next=h[x],h[x]=cnt;
}
int hd[N],ng[N],ans[N],sv[N];
multiset<int> DFS(int u){
    int i;hd[u]=a[u];
    multiset<int> S,F;S.clear();
    for(i=h[u];i;i=w[i].next){
        int e=w[i].to;
        F=DFS(e);hd[u]+=hd[e];
        if(!S.size())S=F,ng[u]=ng[e];
        else{
            auto it=S.begin(),jt=F.begin();
            int pos=0;
            while(it!=S.end()&&jt!=F.end()){
                sv[++pos]=*it+*jt;it++;jt++;
            }
            S.clear();ng[u]=0;
            while(pos){
                if(sv[pos]<0)ng[u]+=sv[pos];
                S.insert(sv[pos]),pos--;
            }
        }
    }
    if(a[u]==0)S.insert(1);else S.insert(-1),ng[u]--;
    ans[u]=hd[u]+ng[u];return S;
}
int main(){
    int i,x;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);scanf("%s",s+1);cnt=0;
        for(i=1;i<=n;i++)a[i]=s[i]-'0',h[i]=ng[i]=ans[i]=hd[i]=0;
        for(i=2;i<=n;i++)scanf("%d",&x),Add(x,i);DFS(1);
        for(i=1;i<=n;i++)printf("%d ",ans[i]);putchar('\n');
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 9900kb

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
Time Limit Exceeded

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...

result: