正文
增加 20 行删除 20 行
63 行未改动6465666768697071727374757677787980818283848586875 行未改动9394959697989910010110210310410510610710815 行未改动1241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771784 行未改动18318418518618718818919019119219319419519619719819920063 行未改动// 计算以u为根的子树在限制下的方案数long long dfs(int u, int black_limit) {int m = children[u].size();// 如果没有子节点if (m == 0) {if (black_limit == 0 || black_limit == 1) return 1;return 0;}// DP数组:dp[i][j]表示处理前i个子节点,有j个黑点的方案数vector<vector<long long>> dp(m + 1, vector<long long>(subtree_size[u] + 1, 0));dp[0][0] = 1;for (int i = 0; i < m; i++) {int child = children[u][i];for (int j = 0; j <= subtree_size[u]; j++) {if (dp[i][j] == 0) continue;// 枚举当前子节点的黑点数量for (int k = 0; k <= subtree_size[child]; k++) {if (has_restriction[child]) {if (k == restrictions[child]) {5 行未改动}}}}long long result = 0;for (int j = 0; j <= subtree_size[u]; j++) {if (j == black_limit) {result = (result + dp[m][j]) % MOD;}}return result;}// 处理Z类型查询15 行未改动for (int child : children[k]) {temp_restriction.push_back(has_restriction[child]);has_restriction[child] = false;}long long result = dfs(k, restrictions[k]);// 恢复限制状态int idx = 0;for (int child : children[k]) {has_restriction[child] = temp_restriction[idx++];}return result;}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n >> p;// 读取树结构for (int i = 0; i < n - 1; i++) {int u, v;cin >> u >> v;tree[u].push_back(v);tree[v].push_back(u);}// 构建有根树build_tree(1, 0);// 读取限制for (int i = 0; i < p; i++) {int x, y;cin >> x >> y;restrictions[x] = y;has_restriction[x] = true;}// 初始化组合数init_comb(n);cin >> q;vector<long long> answers;for (int i = 1; i <= q; i++) {char type;cin >> type;if (type == 'Z') {int k, c;cin >> k >> c;long long ans = solve_Z(k, c);4 行未改动long long ans = solve_H(k);answers.push_back(ans);}}// 计算最终输出long long final_xor = 0;for (int i = 0; i < q; i++) {long long product = (i + 1) * answers[i];final_xor ^= product;}cout << final_xor << endl;return 0;}```63 行未改动6465666768697071727374757677787980818283848586875 行未改动9394959697989910010110210310410510610710815 行未改动1241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771784 行未改动18318418518618718818919019119219319419519619719819920063 行未改动// 计算以u为根的子树在限制下的方案数long long dfs(int u, int black_limit) {int m = children[u].size();// 如果没有子节点if (m == 0) {if (black_limit == 0 || black_limit == 1) return 1;return 0;}// DP数组:dp[i][j]表示处理前i个子节点,有j个黑点的方案数vector<vector<long long>> dp(m + 1, vector<long long>(subtree_size[u] + 1, 0));dp[0][0] = 1;for (int i = 0; i < m; i++) {int child = children[u][i];for (int j = 0; j <= subtree_size[u]; j++) {if (dp[i][j] == 0) continue;// 枚举当前子节点的黑点数量for (int k = 0; k <= subtree_size[child]; k++) {if (has_restriction[child]) {if (k == restrictions[child]) {5 行未改动}}}}long long result = 0;for (int j = 0; j <= subtree_size[u]; j++) {if (j == black_limit) {result = (result + dp[m][j]) % MOD;}}return result;}// 处理Z类型查询15 行未改动for (int child : children[k]) {temp_restriction.push_back(has_restriction[child]);has_restriction[child] = false;}long long result = dfs(k, restrictions[k]);// 恢复限制状态int idx = 0;for (int child : children[k]) {has_restriction[child] = temp_restriction[idx++];}return result;}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);cin >> n >> p;// 读取树结构for (int i = 0; i < n - 1; i++) {int u, v;cin >> u >> v;tree[u].push_back(v);tree[v].push_back(u);}// 构建有根树build_tree(1, 0);// 读取限制for (int i = 0; i < p; i++) {int x, y;cin >> x >> y;restrictions[x] = y;has_restriction[x] = true;}// 初始化组合数init_comb(n);cin >> q;vector<long long> answers;for (int i = 1; i <= q; i++) {char type;cin >> type;if (type == 'Z') {int k, c;cin >> k >> c;long long ans = solve_Z(k, c);4 行未改动long long ans = solve_H(k);answers.push_back(ans);}}// 计算最终输出long long final_xor = 0;for (int i = 0; i < q; i++) {long long product = (i + 1) * answers[i];final_xor ^= product;}cout << final_xor << endl;return 0;}```