求助,85pts,不知为何8~10点WA了
查看原帖
求助,85pts,不知为何8~10点WA了
96340
AC机楼主2021/10/19 21:08
#include<stdio.h>
#include<cctype>
#define int long long
using namespace std;
inline int read()
{
	int x = 0;short w = 0;char ch = 0;
	while(!isdigit(ch)) {w |= ch == '-';ch = getchar();}
	while(isdigit(ch)) {x = (x << 3) + (x << 1) + (ch ^ 48);ch = getchar();}
	return w ? -x : x;
}
int n;
int tot;
const int N = 5e5;
int head[N + 5];
struct Edge
{
	int to;
	int nxt;
};
Edge e[N + 5];
int fa[N + 5];
inline void addEdge(int from, int to)
{
	e[++tot].nxt = head[from];
	e[tot].to = to;
	head[from] = tot;
}
char s[N + 5];
int ans[N + 5];
int sum[N + 5];
int sta[N + 5];
int top;
void dfs(int x)
{
	int flag = 0;
	if (s[x] == ')')
	{
		if (top)
		{
			flag = sta[top];
			ans[x] = ans[fa[flag]] + 1;
			--top;
		}
	}
	else
	{
		sta[++top] = x;
	}
	sum[x] = sum[fa[x]] + ans[x];
	for (int i = head[x]; i; i = e[i].nxt)
	{
		dfs(e[i].to);
	}
	if(flag) s[++top] = flag;
	else
	{
		if(top) --top;
	}
}
signed main()
{
	n = read();
	scanf("%s", s + 1);
	for (int i = 1; i < n; ++i)
	{
		fa[i + 1] = read();
		addEdge(fa[i + 1], i + 1);
	}
	dfs(1);
	int lst = 0;
	for (int i = 2; i <= n; ++i)
	{
		lst ^= (i * sum[i]);
	}
	printf("%lld\n", lst);
	return 0;
}
2021/10/19 21:08
加载中...