QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#592898#7736. Red Black Treedaduoli#WA 117ms27024kbC++231.4kb2024-09-27 09:40:492024-09-27 09:40:50

Judging History

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

  • [2024-09-27 09:40:50]
  • 评测
  • 测评结果:WA
  • 用时:117ms
  • 内存:27024kb
  • [2024-09-27 09:40:49]
  • 提交

answer

#include<bits/stdc++.h>
#define psb push_back
using namespace std;
typedef long long LL;
inline int read(){
	char ch=getchar();
	while(!isdigit(ch) && ch!='-') ch=getchar();
	int x=0,ff=1; if(ch=='-') ff=-1,ch=getchar();
	while(isdigit(ch)) x=(x<<3) + (x<<1) + (ch^48),ch=getchar();
	return x*ff;
}
const int N=1e5+5,inf=1e9;
int n,fa[N],F[N],vd[N],zl[N],zr[N],b[N]; vector<int> e[N],f[N]; char ch[N];
void dfs(int u){
	vd[u]=n; f[u].clear();
	for(int v:e[u]) {
		dfs(v),vd[u]=min(vd[u],vd[v]+1);
		if(ch[u]=='0') ++zr[v];
		else ++zl[v];
	}
	if(!e[u].size()) {
		vd[u]=1; zl[u]=zr[u]=0; F[u]=0;
		if(ch[u]=='0') f[u].psb(0),f[u].psb(1);
		else f[u].psb(1),f[u].psb(0);
	}
	else if((int)e[u].size()==1){
		int v=e[u][0]; swap(f[u],f[v]);
		zl[u]=zl[v]; zr[u]=zr[v]; F[u]=F[v];
	} else{
		f[u].resize(vd[u]+1); F[u]=inf;
		for(int v:e[u]){
			int tt=-1;
			for(int i=1;i<=zl[v];i++) b[++tt]=f[v][0]+zl[v]-i+1;
			for(int x:f[v]) b[++tt]=x;
			for(int i=1;i<=zr[v];i++) b[++tt]=f[v].back()+i;
			for(int i=0;i<=vd[u];i++) f[u][i]=min(f[u][i]+b[i],inf);
		} zl[u]=zr[u]=0; for(int i=0;i<=vd[u];i++) F[u]=min(F[u],f[u][i]);
	}
}
void vmain(){
	n=read(); scanf("%s",ch+1); for(int i=1;i<=n;i++) e[i].clear();
	for(int i=2;i<=n;i++) fa[i]=read(),e[fa[i]].psb(i);
	dfs(1);
	for(int i=1;i<=n;i++) printf("%d ",F[i]);
	puts("");
}
int main(){
	int T=read(); while(T--) vmain();	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 117ms
memory: 27024kb

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 
7 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
2 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 
7 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 
7 1 0 1 0 0 0 0 0 0 0 0 0 0 
2 0 0 0 0 0 0 0 0...

result:

wrong answer 2nd lines differ - expected: '6 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0', found: '7 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 '