跳到正文

题解

题解:CF1521D Nastia Plays with a Tree

AmaoFox约 11 分钟
原文

这是 2026年4月3日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年4月3日
更新
2026年4月4日
篇幅
约 11 分钟
题目
CF1521D

版本

  1. v2当前
  2. v1

题目描述

给定一棵有 nn 个节点的树,通过移除现有边和添加新边(次数不限),将其转化为一个链。求最小操作次数 kk,以及 kk 个具体操作(每个操作移除一条现有边并添加一条新边)。

思路

使用树 DP 计算最小代价(移除边数):

  • 以 11 为根建树。
  • dpu,0dp_{u,0}:uu 不连父亲的最小代价(可连 22 个儿子)。
  • dpu,1dp_{u,1}:uu 连父亲的最小代价(可连 11 个儿子)。

计算:

  • 叶子:dpu,0=dpu,1=0dp_{u,0}=dp_{u,1}=0。
  • 非叶子:排序儿子按 (dpv,0−dpv,1)(dp_{v,0}-dp_{v,1}) 降序(优先连从 00 改成 11 开销最小的儿子)。
  • dpu,1=min⁡(全断儿子+儿子数量,连最佳儿子+儿子数量−1−(dp最佳,0−dp最佳,1))dp_{u,1} = \min({\footnotesize全断儿子} + {\footnotesize儿子数量}, {\footnotesize连最佳儿子} + {\footnotesize儿子数量} - 1 - (dp_{{\tiny最佳},0} - dp_{{\tiny最佳},1}))。
  • dpu,0=min⁡(两个最佳儿子+儿子数量−2−收益和,dpu,1)dp_{u,0} = \min({\footnotesize两个最佳儿子} + {\footnotesize儿子数量} - 2 - {\footnotesize收益和}, dp_{u,1})。

根的 dp1,0dp_{1,0} 即答案 kk。

构造方案只需要回溯 DP 决策,收集移除了哪些父子之间的边,重建一个保留了这些边的图。

然后找到每条链的端点(度数 ≤1\le 1),连接成链(添加边),每个移除配一个添加即可。

代码

AI 真的太好用了你们知道吗(?)
C++
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_N = 100005;

int nodes, tests;
vector<int> tree[MAX_N], edges[MAX_N];
int dp[MAX_N][2];
int current_tag;
vector<pair<int, int>> remove_edges, add_edges;
int degree[MAX_N];
bool visited[MAX_N];

bool compare_nodes(int a, int b) {
    return (dp[a][0] - dp[a][1]) > (dp[b][0] - dp[b][1]);
}

void build_tree(int node, int parent) {
    for (int child : edges[node]) {
        if (child != parent) {
            tree[node].push_back(child);
            build_tree(child, node);
        }
    }
}

void compute_dp(int node) {
    int child_count = 0;
    long long total = 0;
    for (int child : tree[node]) {
        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, 
                     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[second_child][0] - dp[second_child][1]) + child_count - 2;
    } else {
        dp[node][0] = dp[node][1];
    }
    dp[node][0] = min(dp[node][0], dp[node][1]);
}

void add_edge(int a, int b) {
    edges[a].push_back(b);
    edges[b].push_back(a);
    degree[a]++;
    degree[b]++;
}

void construct_solution(int node, int state) {
    int child_count = 0;
    long long total = 0;
    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);
                remove_edges.push_back({node, child});
            }
        } else {
            construct_solution(first_child, 1);
            add_edge(node, first_child);
            for (int i = 1; i < tree[node].size(); i++) {
                construct_solution(tree[node][i], 0);
                remove_edges.push_back({node, tree[node][i]});
            }
        }
        return;
    }
    
    if (tree[node].size() >= 2) {
        construct_solution(first_child, 1);
        add_edge(node, first_child);
        construct_solution(second_child, 1);
        add_edge(node, second_child);
        for (int i = 2; i < tree[node].size(); i++) {
            construct_solution(tree[node][i], 0);
            remove_edges.push_back({node, tree[node][i]});
        }
    } else {
        if (dp[node][0] == total + child_count) {
            for (int child : tree[node]) {
                construct_solution(child, 0);
                remove_edges.push_back({node, child});
            }
        } else {
            construct_solution(first_child, 1);
            add_edge(node, first_child);
            for (int i = 1; i < tree[node].size(); i++) {
                construct_solution(tree[node][i], 0);
                remove_edges.push_back({node, tree[node][i]});
            }
        }
    }
}

void mark_component(int node, int parent) {
    visited[node] = true;
    current_tag = node;
    for (int neighbor : edges[node]) {
        if (neighbor != parent) {
            mark_component(neighbor, node);
        }
    }
}

void process_case() {
    cin >> nodes;
    for (int i = 0; i <= nodes; i++) {
        degree[i] = dp[i][0] = dp[i][1] = visited[i] = 0;
        tree[i].clear();
        edges[i].clear();
    }
    remove_edges.clear();
    add_edges.clear();
    for (int i = 1; i < nodes; i++) {
        int a, b;
        cin >> a >> b;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    build_tree(1, 0);
    for (int i = 0; i <= nodes; i++) {
        edges[i].clear();
    }
    compute_dp(1);
    construct_solution(1, 0);
    int previous = 0;
    for (int i = 1; i <= nodes; i++) {
        if ((degree[i] <= 1) && !visited[i]) {
            mark_component(i, 0);
            if (previous != 0) {
                add_edges.push_back({previous, i});
            }
            previous = current_tag;
        }
    }
    cout << dp[1][0] << '\n';
    for (int i = 0; i < remove_edges.size(); i++) {
        cout << remove_edges[i].first << ' ' << remove_edges[i].second << ' '
             << add_edges[i].first << ' ' << add_edges[i].second << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    
    cin >> tests;
    while (tests--) {
        process_case();
    }
    return 0;
}