# 题解

sum[i]表示到i为止，有多少个括号（）是串联的

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
const int MAXN = 5e5 + 3;
int n , fa[MAXN];
vector<int>G[MAXN];
ll sum[MAXN] , ans[MAXN];
int sta[MAXN] , cnt;
int zf[MAXN] , cntz;
char a[MAXN];
void dfs( int x ){
for( int i = 0; i < G[x].size() ; i ++ ){
int v = G[x][i];
if( v == fa[x] ) continue;
ans[v] = ans[x];
if( a[v] == '(' ){
sta[++cnt] = v;
zf[++cntz] = v;
dfs( v );
sta[cnt--] = 0;zf[cntz--] = 0;
continue;
}
int y = zf[cntz];
bool fl = 0;
if( cntz > 0 )
ans[v] ++ , zf[cntz--]= 0 , fl = 1;
ans[v] += sum[fa[y]];
sum[v] = sum[fa[y]] + ( fl == 1 ? 1 : 0 );
sta[++cnt] = v;
dfs( v );
sum[v] = 0;
sta[cnt--] = 0;
if( fl )
zf[++cntz] = y;
}
}
int main()
{
scanf( "%d" , &n );
scanf( "%s" , a + 1 );
for( int i = 2 ; i <= n  ; i++ ){
scanf( "%d" , &fa[i] );
G[fa[i]].push_back( i );
G[i].push_back( fa[i] );
}
sum[1] = ans[1] = 0;
sta[++cnt] = 1;
if( a[1] == '(' )
zf[++cntz] = 1;
dfs( 1 );
ll o = 1ll * ans[1] * 1;
for( int i = 2 ; i <= n ; i ++ )
o = o ^ ( 1ll * i * ans[i] );
printf( "%lld" , o );
return 0;
}


11-28 222
11-30 136

10-13 1091
11-30 70
03-23 114
01-23 52
11-14 807