QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#401461#7736. Red Black Treephil071128WA 1ms3816kbC++231.4kb2024-04-28 18:57:142024-04-28 18:57:14

Judging History

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

  • [2024-04-28 18:57:14]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3816kb
  • [2024-04-28 18:57:14]
  • 提交

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