80pts求条
查看原帖
80pts求条
375418
自由软件Free楼主2024/10/22 21:45
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
char a[500005];
int fa[500005], k[500005];
int last[500005];
vector<int> child[500005];
void dfs(int no)
{
    if (no != 1)
    {
        k[no] = k[fa[no]];
        if (a[no] == ')')
        {
            int rs = 1, tmp = last[fa[no]];
            while (tmp != 0)
            {
                if (a[tmp] == '(')
                    --rs;
                else
                    ++rs;
                if (rs == 0)
                    ++k[no], tmp = fa[tmp];
                else if (rs == 1)
                    tmp = last[fa[tmp]];
                else if (rs < 0)
                {
                    last[no] = tmp;
                    break;
                }
                else
                    tmp = fa[tmp];
            }
        }
        else
            last[no] = no;
    }
    else if (a[1] == '(')
        last[1] = 1;
    for (int i : child[no])
        dfs(i);
}
signed main()
{
    scanf("%lld\n", &n);
    for (int i = 1; i <= n; ++i)
        a[i] = getchar();
    for (int i = 2; i <= n; ++i)
    {
        scanf("%lld", &fa[i]);
        child[fa[i]].emplace_back(i);
    }
    dfs(1);
    int anss = k[1];
    for (int i = 2; i <= n; ++i)
        anss = anss ^ (i * k[i]);
    printf("%lld\n", anss);
    return 0;
}
2024/10/22 21:45
加载中...