QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#401461 | #7736. Red Black Tree | phil071128 | WA | 1ms | 3816kb | C++23 | 1.4kb | 2024-04-28 18:57:14 | 2024-04-28 18:57:14 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,multiset<int> >
#define fi first
#define se second
using namespace std;
int read(){
char c=getchar();int h=0,tag=1;
while(!isdigit(c)) tag=(c=='-'?-1:1),c=getchar();
while(isdigit(c)) h=(h<<1)+(h<<3)+(c^48),c=getchar();
return h*tag;
}
void fil(){
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
}
const int N=1e5+505;
vector<int>s[N];
int ans[N],fa[N];
char c[N];
multiset<int>t3;
pii dfs(int x) {
pii tmp;tmp.fi=0,tmp.se.clear();
bool tag=0;
for(int y:s[x]) {
if(y==fa[x]) continue;
pii to=dfs(y); tmp.fi+=to.fi;
if(!tag) {
tmp.se=to.se;tag=1;continue;
}
vector<int>v1,v2;
for(int t:tmp.se) v1.push_back(t);
for(int t:to.se) v2.push_back(t);
t3.clear();
for(int i=0;i<min(v1.size(),v2.size());i++) t3.insert(v1[i]+v2[i]);
tmp.se=t3;
}
int sum=0;
if(c[x]=='0') tmp.se.insert(1); //red
else tmp.se.insert(-1),tmp.fi++;
for(int t:tmp.se) if(t<0) sum+=t;
ans[x]=tmp.fi+sum;
return tmp;
}
int sumn;
void solve() {
int n=read();
sumn+=n;
for(int i=1;i<=n;i++) fa[i]=0,s[i].clear(),ans[i]=0,c[i]=0;
scanf("%s",c+1);
for(int i=2;i<=n;i++) fa[i]=read(),s[i].push_back(fa[i]),s[fa[i]].push_back(i);
pii x=dfs(1);
for(int i=1;i<=n;i++) {
printf("%d ",ans[i]);
}
printf("\n");
}
int main(){
//fil();
int T=read();
for(int i=1;i<=min(T,6103);i++) solve();
cout<<sumn<<endl;
return 0;
}
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3816kb
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 13
result:
wrong output format Extra information in the output file