QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#266769#7736. Red Black Treetime_interspace#RE 0ms8092kbC++202.7kb2023-11-26 17:31:032023-11-26 17:31:04

Judging History

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

  • [2024-02-19 10:14:05]
  • hack成功,自动添加数据
  • (/hack/538)
  • [2023-11-26 17:31:04]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:8092kb
  • [2023-11-26 17:31:03]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100005
#define MAXS 2000005
const int inf = 0x3f3f3f3f;
int t, n;
vector<int> e[MAXN];
char s[MAXN];
int val[MAXN], nxt[MAXN], tot;
int st[MAXN], pos1[MAXN], pos2[MAXN], ans[MAXN], head[MAXN];
int dep[MAXN];

void push(int x, int y){
	// printf("(%d, %d)\n", x, y);
	e[x].push_back(y);
}

void dfs(int x){
	dep[x] = 0x3f3f3f3f;
	for(auto v : e[x]){
		dfs(v);
		dep[x] = min(dep[x], dep[v] + 1);
	}
	if(dep[x] == 0x3f3f3f3f) dep[x] = 1;
}

void merge(int x, int y){
// printf("merge(%d, %d)\n", x, y);
	int a = head[x], b = head[y];
	st[x] += st[y];
	for(int i=1;i<dep[x];i++){
		a = nxt[a], b = nxt[b];
		val[a] += val[b];
	}
	nxt[a] = 0;
	a = head[x];
	int sum = st[x];
	// printf("#####x = %d, st = %d\n", x, st[x]);
	for(int i=0;i<dep[x];i++){
		// printf("sum = %d, val[a] = %d, val[nxt[a] = %d] = %d\n", sum, val[a], nxt[a], val[nxt[a]]);
		if(val[a] < 0 && val[nxt[a]] >= 0) pos1[x] = a, ans[x] = sum;
		if(val[a] <= 0 && val[nxt[a]] > 0) pos2[x] = a;
		a = nxt[a];
		sum += val[a];
	}
	// puts("");
}

void show(int x){
	printf("x = %d, dep[x] = %d, st = %d, ans = %d\n", x, dep[x], st[x], ans[x]);
	int now = nxt[head[x]];
	while(now) printf("%d ", val[now]), now = nxt[now]; puts("");

}

void dfs1(int x){
	// printf("x = %d\n", x);
	if(e[x].size() == 0){
		ans[x] = 0;
		head[x] = tot+1;
		if(s[x] == '0'){
			st[x] = 0;
			val[++tot] = -inf;
			++tot;
			nxt[tot-1] = tot;
			val[tot] = 1;
			pos1[x] = pos2[x] = tot-1;
		}else{
			st[x] = 1;
			val[++tot] = -inf;
			++tot;
			nxt[tot-1] = tot;
			val[tot] = -1;
			pos1[x] = pos2[x] = tot;
		}
		// printf("##x = %d, st = %d, ans = %d\n", x, st[x], ans[x]);
		// int now = nxt[head[x]];
		// while(now) printf("%d ", val[now]), now = nxt[now]; puts("");
		return;
	}
	ans[x] = n;
	dfs1(e[x][0]);
	st[x] = st[e[x][0]], pos1[x] = pos1[e[x][0]];
	pos2[x] = pos2[e[x][0]], ans[x] = ans[e[x][0]];
	head[x] = head[e[x][0]];
	// show(x);
	for(int i=1;i<e[x].size();i++){
		int v = e[x][i];
		dfs1(v);
		merge(x, v);
		// show(x);
	}
	if(s[x] == '0'){
		int nx = nxt[pos2[x]];
		val[++tot] = 1;
		nxt[pos2[x]] = tot;
		nxt[tot] = nx;
	}else{
		st[x]++;
		int nx = nxt[pos1[x]];
		val[++tot] = -1;
		nxt[pos1[x]] = tot;
		nxt[tot] = nx;
		if(pos2[x] == pos1[x]) pos2[x] = tot;
		pos1[x] = tot;
	}
	// show(x);
}

void work(){
	scanf("%d", &n);
	scanf("%s", s+1);
	int x;
	for(int i=2;i<=n;i++) scanf("%d", &x), push(x, i);
	dfs(1); dfs1(1);
	for(int i=1;i<=n;i++) printf("%d ", ans[i]);
	puts("");
	for(int i=1;i<=tot;i++) val[i] = 0, nxt[i] = 0;
	tot = 0;
	for(int i=1;i<=n;i++) e[i].clear();
}

int main(){
	val[0] = inf;
	cin>>t;
	while(t--) work();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 8092kb

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
Runtime Error

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: