版本比较
题解:P9398 「DBOI」Round 1 烟花
正文
增加 20 行删除 20 行
63 行未改动646566676868697071727374747576777878798081828383848586875 行未改动939495969797989910010110210310410410510610710815 行未改动1241251261271281281291301301311321331341351361361371381391401411421431431441451451461471481491501511521531531541551561561571581591601611621631641641651661671671681691701701711721731741741751761771784 行未改动18318418518618718718818919019119219319419419519619619719819920063 行未改动
// 计算以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;}
```