版本比较
题解:CF1521D Nastia Plays with a Tree
正文
增加 11 行删除 11 行
62 行未改动636465666767686970717272737475767576777878798081818283848513 行未改动991001011021031031041051051061071081081091101111128 行未改动12112212312412512512612712812970 行未改动2002012022032042042052062072084 行未改动62 行未改动 compute_dp(child); total += dp[child][0]; child_count++; }
if (child_count == 0) { dp[node][0] = dp[node][1] = 0; return; }
sort(tree[node].begin(), tree[node].end(), compare_nodes); int first_child = tree[node][0]; dp[node][1] = min(total + child_count,
dp[node][1] = min(total + child_count, total - (dp[first_child][0] - dp[first_child][1]) + child_count - 1);
if (tree[node].size() >= 2) { int second_child = tree[node][1]; dp[node][0] = total - (dp[first_child][0] - dp[first_child][1]) - dp[node][0] = total - (dp[first_child][0] - dp[first_child][1]) - (dp[second_child][0] - dp[second_child][1]) + child_count - 2; } else { dp[node][0] = dp[node][1]; }13 行未改动 for (int child : tree[node]) { total += dp[child][0]; child_count++; }
if (child_count == 0) return;
int first_child = tree[node][0]; int second_child = tree[node].size() >= 2 ? tree[node][1] : 0;
if (state == 1 || dp[node][0] == dp[node][1]) { if (dp[node][1] == total + child_count) { for (int child : tree[node]) { construct_solution(child, 0);8 行未改动 } } return; }
if (tree[node].size() >= 2) { construct_solution(first_child, 1); add_edge(node, first_child); construct_solution(second_child, 1);70 行未改动int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> tests; while (tests--) { process_case(); }4 行未改动