# NOIP2019_Day1T2_括号树
\[\LARGE ---咕咕咕咕咕!---
\]
代码:
贴一张代码好了QAQ
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 500010;
int n;
char a[N]; int fa[N];
int head[N], nxt[N << 1], ver[N << 1], tot;
int sta[N], top;
ll offer[N], sum[N], ans;
void add(int u, int v) {
ver[++tot] = v; nxt[tot] = head[u]; head[u] = tot;
}
void dfs(int now) {
if (a[now] == '(') {
sta[++top] = now;
offer[now] = 0;
sum[now] = sum[fa[now]];
for (int i = head[now]; i; i = nxt[i]) {
int son = ver[i];
dfs(son);
}
top--;
} else {
if (top == 0) {
offer[now] = 0;
sum[now] = sum[fa[now]];
for (int i = head[now]; i; i = nxt[i]) {
int son = ver[i];
dfs(son);
}
} else {
int match = sta[top]; top--;
if (a[fa[match]] == ')') {
offer[now] = offer[fa[match]] + 1;
sum[now] = sum[fa[now]] + offer[now];
} else {
offer[now] = 1;
sum[now] = sum[fa[now]] + 1;
}
for (int i = head[now]; i; i = nxt[i]) {
int son = ver[i];
dfs(son);
}
sta[++top] = match;
}
}
return;
}
int main() {
scanf("%d", &n);
scanf("%s", a + 1);
for (int i = 2; i <= n; i++) {
scanf("%d", &fa[i]);
add(fa[i], i);
}
dfs(1);
for (int i = 1; i <= n; i++) {
ans = ans xor (i * sum[i]);
}
printf("%lld\n", ans);
return 0;
}

浙公网安备 33010602011771号