题解
2026-05-31 19:22:57
发布于:重庆
10阅读
0回复
0点赞
求点赞👍
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
int son[N][2];
int f[N], col[N], sum[N];
void dfs(int x, int now) {
now += sum[x];
if (now & 1) col[x] ^= 1;
for (int i = 0; i < 2; i++) {
if (son[x][i] != -1) dfs(son[x][i], now);
}
}
int main() {
cin >> n;
memset(son, -1, sizeof son);
for (int i = 2; i <= n; i++) {
cin >> f[i];
for (int j = 0; j < 2; j++) {
if (son[f[i]][j] == -1) {
son[f[i]][j] = i;
break;
}
}
}
string s;
cin >> s;
for (int i = 1; i <= n; i++) {
col[i] = s[i - 1] - '0';
}
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
sum[x] += 1;
}
dfs(1, 0);
for (int i = 1; i <= n; i++) {
cout << col[i];
}
cout << "\n";
}
这里空空如也




有帮助,赞一个