QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#611071#7736. Red Black TreeUSTC_fish_touching_teamWA 296ms22948kbC++141.8kb2024-10-04 19:11:542024-10-04 19:11:55

Judging History

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

  • [2024-10-04 19:11:55]
  • 评测
  • 测评结果:WA
  • 用时:296ms
  • 内存:22948kb
  • [2024-10-04 19:11:54]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define Re register int

const int N = 100005, Inf = 1000000;
map<int, int> f[N];
int T, n, cnt, col[N], hea[N], nxt[N], to[N], d[N], dd[N], h[N], ans[N], dq1[N], dq2[N], g1[N], g2[N];
char s[N];

inline void add(int x, int y)
{
	nxt[++cnt] = hea[x], to[cnt] = y, hea[x] = cnt, ++d[x];
}

inline void dfs(int x)
{
	int q[2], y = x, he = 1, v = 0;
	q[0] = q[1] = 0, q[col[x]] = 1, ans[y] = N;
	while (d[x] == 1) x = to[hea[x]], ++q[col[x]], ++he;
	if (!hea[x]) h[y] = he;
	else h[y] = Inf;
	for (Re i = hea[x]; i; i = nxt[i])
	{
		int u = to[i];
		dfs(u);
		h[y] = min(h[y], he + h[u]);
	}
	for (Re i = hea[x]; i; i = nxt[i]) dd[++v] = to[i];
	int l1 = 1, r1 = 0, l2 = 1, r2 = 0;
	for (Re i = 0; i <= h[y]; ++i)
	{
		g1[i] = i, g2[i] = -i;
		if (i > h[y] - he) g1[i] = g2[i] = Inf;
		else for (Re j = 1; j <= v; ++j) g1[i] += f[dd[j]][i], g2[i] += f[dd[j]][i];
		while (l1 <= r1 && i - dq1[l1] > q[1]) ++l1;
		while (l1 <= r1 && g1[dq1[r1]] >= g1[i]) --r1;
		dq1[++r1] = i;
		f[y][i] = g1[dq1[l1]] + q[1] - i;
		if (i >= q[1] + 1)
		{
			while (l2 <= r2 && i - dq2[l2] > q[0] + q[1]) ++l2;
			while (l2 <= r2 && g2[dq2[r2]] >= g2[i - q[1] - 1]) --r2;
			dq2[++r2] = i - q[1] - 1;
			f[y][i] = min(f[y][i], g2[dq2[l2]] + i - q[1]);
		}
		ans[y] = min(ans[y], f[y][i]);
		//printf("!!%d %d %d\n", y, i, f[y][i]);
	}
	x = y;
	while (d[x] == 1) x = to[hea[x]], ans[x] = ans[y];
}

int main()
{
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d", &n);
		scanf("%s", s + 1);
		cnt = 0;
		for (Re i = 1; i <= n; ++i) col[i] = (s[i] == 49), hea[i] = d[i] = 0, f[i].clear();
		for (Re i = 2; i <= n; ++i)
		{
			int u;
			scanf("%d", &u);
			add(u, i);
		}
		dfs(1);
		for (Re i = 1; i < n; ++i) printf("%d ", ans[i]);
		printf("%d\n", ans[n]);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 296ms
memory: 22948kb

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 0 0 0 0
5 3 ...

result:

wrong answer 122nd lines differ - expected: '4 1 1 0 0 0 0 0 0 0 0 0', found: '3 1 1 0 0 0 0 0 0 0 0 0'