代码如下,帮一下吧
#include <bits/stdc++.h>
using namespace std;
struct node {
int a, b;
int f = false;
} root[100005]={};
string s;
int n, q;
void dfs(int step) {
if (root[step].a==0 && root[step].b==0) return ;
if (root[step].a!=0) {
s[root[step].a] = (s[root[step].a]-'0')^1+'0';
dfs(root[step].a);
}
if (root[step].b!=0) {
s[root[step].b] = (s[root[step].b]-'0')^1+'0';
dfs(root[step].b);
}
}
int main() {
cin >> n;
for (int i = 1;i<= n-1;i++) {
int a;
cin >> a;
if (root[a].a > 0 )root[a].b = i+1;
else root[a].a = i + 1;
}
cin >> s >> q;
s = " "+s;
while (q--) {
int a; cin >> a;
root[a].f ^= 1;
}
for (int i = 1;i<= n;i++) {
if (root[i].f) {
s[i] = (s[i]-'0')^1+'0';
dfs(i);
}
}
cout << s.substr(1) << "\n";
return 0;
}