QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#680707 | #7736. Red Black Tree | Yoralen | WA | 1ms | 5904kb | C++14 | 1.3kb | 2024-10-26 22:16:07 | 2024-10-26 22:16:07 |
Judging History
answer
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
const int N=100005;
struct edge{int to,next;}w[N<<1];
int n,h[N],cnt,T,a[N],ar[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,j;hd[u]=a[u];
multiset<int> S,F;S.clear();
int flg=0,ln;
for(i=h[u];i;i=w[i].next){
int e=w[i].to;
F=move(DFS(e));hd[u]+=hd[e];
if(!S.size())S=move(F),ng[u]=ng[e],ln=F.size();
else{
auto it=S.begin(),jt=F.begin();
for(j=1;j<=ln&&jt!=F.end();j++){
if(!flg){
ar[j]=*it+*jt;
it++,jt++;
}
else ar[j]+=*jt,jt++;
}
ln=min(ln,int(F.size()));flg=1;
}
}
if(flg){
S.clear();ng[u]=0;
for(i=1;i<=ln;i++){
if(ar[i]<0)ng[u]+=ar[i];
S.insert(ar[i]);
}
}
if(a[u]==0)S.insert(1);else S.insert(-1),ng[u]--;
ans[u]=hd[u]+ng[u];return move(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;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 5904kb
input:
2 9 101011110 1 1 3 3 3 6 2 2 4 1011 1 1 3
output:
5 1 3 0 0 0 0 0 0 2 0 0 0
result:
wrong answer 1st lines differ - expected: '4 1 2 0 0 0 0 0 0', found: '5 1 3 0 0 0 0 0 0 '