求助大佬!!洛谷上能过,本地测试时dfs爆了
查看原帖
求助大佬!!洛谷上能过,本地测试时dfs爆了
478755
Karis楼主2021/10/1 16:28
#include<iostream>
#include<cstdio>
#include<cstring>
#define R register
#define ll unsigned long long 
#define N 500010

using namespace std;

int read()
{
	R int x = 0, f = 1;
	R char ch = getchar();
	while(ch > '9' || ch < '0')
	{
		if(ch == '-')
			f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
	{
		x = x*10 + ch-'0';
		ch = getchar();
	}
	return x*f;
}

ll n, head[N], tot, dp[N], lst[N], gx[N];
ll ans = 0;

struct node{
	int fa;
	char ch;
}p[N];

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

void add(int u, int v)
{
	e[++tot].to = v;
	e[tot].next = head[u];
	head[u] = tot;
}

void update(int x)
{
	if(p[x].ch == '(')
		lst[x] = x, dp[x] = dp[p[x].fa], gx[x] = 0;
	else
	{
		if(lst[p[x].fa] > 0)
		{
			lst[x] = lst[p[lst[p[x].fa]].fa];
			gx[x] = gx[p[lst[p[x].fa]].fa] + 1;
			dp[x] = dp[p[x].fa] + gx[x];
		}
		else
		{
			lst[x] = 0;
			gx[x] = 0;
			dp[x] = dp[p[x].fa];
		}
	}
	return;
}

void dfs(int x)
{
	if(p[x].ch == '(')
		lst[x] = x, dp[x] = dp[p[x].fa], gx[x] = 0;
	else
	{
		if(lst[p[x].fa] > 0)
		{
			lst[x] = lst[p[lst[p[x].fa]].fa];
			gx[x] = gx[p[lst[p[x].fa]].fa] + 1;
			dp[x] = dp[p[x].fa] + gx[x];
		}
		else
		{
			lst[x] = 0;
			gx[x] = 0;
			dp[x] = dp[p[x].fa];
		}
	}
	for(R int i = head[x];i;i = e[i].next)
		dfs(e[i].to);
	return; 
}

int main()
{
	freopen("brackets.in","r",stdin);freopen("brackets.out","w",stdout);
	n = read();
	for(R int i = 1; i <= n; ++i)
		scanf("%c",&p[i].ch);
	for(R int i = 2; i <= n; ++i)
		p[i].fa = read(), add(p[i].fa, i);
	dfs(1);
	for(R ll i = 1; i <= n; ++i)
		ans = ans xor (i*dp[i]);
	printf("%lld\n", ans);
	return 0;
	fclose(stdin);fclose(stdout);
}

/*
8
)())((()
1 2 3 4 5 6 7

*/
2021/10/1 16:28
加载中...