求助!!样例能过,全wa
查看原帖
求助!!样例能过,全wa
467683
return_CE楼主2022/10/23 10:56
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int n,l,r,h[500005],f[500005],lt[500005],rt[500005],cnt;
char a[500005];
ll ans[500005],ans2;
struct point
{
	int nex,to;
}edg[2000020];
void add(int x,int y)
{
	edg[++cnt].to=y;
	edg[cnt].nex=h[x];
	h[x]=cnt;
}
void dfs(int p)
{
	for(int i=h[p];i;i=edg[i].nex )
	{
		int to=edg[i].to;
		 rt[p]=r;
		 lt[p]=l;
		if(l==r)
		{
			if(a[to]=='(')
			{
				ans[to]=ans[f[to]];
				l=1;r=0;
			}
			else
			{
				
				ans[to]=ans[f[to]];
				l=0;r=0;
				ans2=0;
			}
		}
		else
		{
			if(l>r)
			{
				if(a[to]=='(')
				{
					l++;
					ans[to]=ans[f[to]];
					
				}
				else
				{
					ans[to]=ans[f[to]]+ans2+1;
				//	ans[to]=ans[f[to]]+1;
					r++;
					if(l==r)
					{
						ans2++;
						l=0;r=0;
					//	ans2++;
					}
				}
			}
			else 	//r>l
			{
				if(a[to]=='(')
				{
					ans[to]=ans[f[to]];
					l=1;r=0;
					ans2=0;
				}
				else
				{
					ans[to]=ans[f[to]];
					l=0;r=0;
					ans2=0;
				}
			}
		}
		dfs(to);
		l=lt[p];
		r=rt[p];
	}
}
int main()
{
	cin>>n;
	scanf("%s",a+1);
	for(int i=2;i<=n;i++)
	{
		int x,y;
		scanf("%d",&x);
		f[i]=x;
		add(x,i);
	}
	if(a[1]=='(')l++;
	else r++;
	lt[1]=l;rt[1]=r;
	ans[1]=0;
	dfs(1);
	ll summ;
//	for(int i=1;i<=n;i++)cout<<ans[i]<<endl;
	for(int i=1;i<=n;i++) 
		summ^=ans[i]*(ll)i;
	cout<<summ;
	return 0;
}
/*
5
(()()
1 1 2 2

6
*/
2022/10/23 10:56
加载中...