Files
python/GESP/Level6/20240602_3.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

58 lines
1.4 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
int son[N][2]; // 记录每个节点的两个儿子
int fa[N]; // 记录每个节点的父节点是哪个
int col[N]; // 记录每个节点的颜色
int sum[N]; // 记录每个节点被操作的次数
void dfs(int x, int now) {
now += sum[x]; // 递归计算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() {
#ifndef ONLINE_JUDGE
freopen("20240602.in", "r", stdin);
#endif
cin >> n; // n个节点
memset(son, -1, sizeof son); // 初始化son数组,-1表示没有儿子
// 这个i从2开始是因为题目中要求的意义
for (int i = 2; i <= n; i++) {
cin >> fa[i];
for (int j = 0; j < 2; j++) { // 循环两次,因为每个节点有两个儿子
if (son[fa[i]][j] == -1) {
son[fa[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; // x这个节点被操作了一次
}
// 从根节点开始dfs
dfs(1, 0);
// 输出每个节点的颜色
for (int i = 1; i <= n; i++)
cout << col[i];
return 0;
}