题目描述
给定一棵有 个节点的树,通过移除现有边和添加新边(次数不限),将其转化为一个链。求最小操作次数 ,以及 个具体操作(每个操作移除一条现有边并添加一条新边)。
思路
使用树 DP 计算最小代价(移除边数):
- 以 为根建树。
- : 不连父亲的最小代价(可连 个儿子)。
- : 连父亲的最小代价(可连 个儿子)。
计算:
- 叶子:。
- 非叶子:排序儿子按 降序(优先连从 改成 开销最小的儿子)。
- 。
- 。
根的 即答案 。
构造方案只需要回溯 DP 决策,收集移除了哪些父子之间的边,重建一个保留了这些边的图。
然后找到每条链的端点(度数 ),连接成链(添加边),每个移除配一个添加即可。
代码
AI 真的太好用了你们知道吗(?)
#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;
}