玄学WA
查看原帖
玄学WA
765883
__Tao__楼主2022/10/18 12:19

点8~10WA,数据下下来测试时发现结果居然与常数N有关?

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5e5 + 5;
char a[N];
int fa[N], h[N], cnt, b, n, t[N], top;
ll ans, f[N], sum[N];

struct edge {
	int next, to;
} r[N];

void add(int from, int to) {
	r[++cnt].to = to;
	r[cnt].next = h[from];
	h[from] = cnt;
}

void dp(int x) {
	int p = 0;
	if (a[x] == '(') {
		t[++top] = x;
	} else if (top) {
		p = t[top--];
		f[x] = f[fa[p]] + 1;
	}
	sum[x] = sum[fa[x]] + f[x];
	for (int i = h[x]; i; i = r[i].next)
		dp(r[i].to);
	if (p)
		t[++top] = p;
	else
		top--;
}

int main() {
	//freopen("P5658_8.in", "r", stdin);
	scanf("%d", &n);
	scanf("%s", a + 1);
	for (int i = 2; i <= n; i++) {
		scanf("%d", &b);
		add(b, i);
		fa[i] = b;
	}
	dp(1);
	for (int i = 1; i <= n; i++)
		ans = (sum[i] * (ll)i)^ans;//cout << i << " " << f[i] << endl;
	printf("%lld", ans);
	return 0;
}
2022/10/18 12:19
加载中...