1 条题解

  • 0
    @ 2022-8-12 16:46:24

    #include <iostream>

    using namespace std; long long n, h[5000010], w[5000010], fa[5000000], ans, cnt, head[5000010], to[5000010], nxt[5000010]; char s[5000010]; void dfs(long long x){ w[x] = w[fa[x]]; if(s[x] == '(') w[x] = x; else if(w[x]){ h[x] = 1 + h[fa[w[x]]]; w[x] = w[fa[w[x]]]; } for(int i = head[x]; i; i = nxt[i]) dfs(to[i]); } int main(){ cin >> n; cin >> (s + 1); long long x; for(long long i = 2;i <= n;i++){ cin >> x; fa[i] = x; cnt++; nxt[cnt] = head[x]; head[x] = cnt; to[cnt] = i; } dfs(1); ans = h[1]; for(long long i = 2;i <= n;i++){ h[i] += h[fa[i]]; ans ^= (i * h[i]); } cout << ans << endl; }

    • 1

    信息

    ID
    4581
    时间
    1000ms
    内存
    250MiB
    难度
    4
    标签
    递交数
    8
    已通过
    4
    上传者