hduoj - 6567 Cotree【树DP】

本文探讨了在一个由两棵不相连的树组成的图中,通过添加一条边来最小化所有节点间路径长度总和的问题。采用两次DFS算法预处理节点距离之和,找到连接两树的最佳节点对,最终输出最小化的路径总和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cotree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 33    Accepted Submission(s): 13


 

Problem Description

Avin has two trees which are not connected. He asks you to add an edge between them to make them connected while minimizing the function ∑ni=1∑nj=i+1dis(i,j), where dis(i,j) represents the number of edges of the path from i to j. He is happy with only the function value.

 

 

Input

The first line contains a number n (2<=n<=100000). In each of the following n−2 lines, there are two numbers u and v, meaning that there is an edge between uand v. The input is guaranteed to contain exactly two trees.

 

 

Output

Just print the minimum function value.

 

 

Sample Input


 

3 1 2

 

 

Sample Output


 

4

 

 

分析:对每棵树分别求出每个节点到其他节点的距离之和,显而易见,两棵树上这个值最小的点连起来就是最优解。

然后再跑一次整棵树答案就出来了。可以先跑出第一个节点到其他节点的距离之和,然后维护一个子树的节点数不断的更新,随便推一下结果就出来了。

#include<bits/stdc++.h>

using namespace std;
int n;
vector<int> v[100004];
long long son[100004];
bool vis[100004];
long long size[100004];
int fa[100004];
int num[100004];

int Find(int a) {
    return fa[a] == a ? a : fa[a] = Find(fa[a]);
}

void dfs1(int u, int pre) {
    son[u] = 1;
    for (int i = 0; i < v[u].size(); ++i) {
        if (v[u][i] == pre)continue;
        dfs1(v[u][i], u);
        son[u] += son[v[u][i]];
    }
}

long long sum, res;

void dfs2(int u, int pre, int n) {
    size[u] = res;
    sum++;
    for (int i = 0; i < v[u].size(); ++i) {
        if (v[u][i] == pre)continue;
        res += sum;
        res += -2 * son[v[u][i]] + n - sum;
        dfs2(v[u][i], u, n);
    }
    res += -sum + 2 * son[u];
    res += -(n - sum);
}

void dfs3(int u, int pre, int dis) {
    res += dis;
    for (int i = 0; i < v[u].size(); ++i) {
        if (v[u][i] == pre)continue;
        dfs3(v[u][i], u, dis + 1);
    }

}

int main() {
    cin >> n;
    memset(son, 0, sizeof(son));
    memset(vis, 0, sizeof(vis));
    for (int i = 1; i <= n; ++i) {
        fa[i] = i;
        num[i] = 1;
    }
    for (int i = 0; i < n - 2; ++i) {
        int x, y;
        scanf("%d%d", &x, &y);
        v[x].push_back(y);
        v[y].push_back(x);
        int fx = Find(x);
        int fy = Find(y);
        if (fx != fy) {
            fa[fx] = fy;
            num[fy] += num[fx];
        }
    }
    vector<int> a;
    for (int i = 1; i <= n; ++i) {
        if (fa[i] == i) {
            a.push_back(i);
            sum = res = 0;
            dfs1(i, -1);
            dfs3(i, -1, 0);
            dfs2(i, -1, num[i]);
        }
    }
    long long mini1 = 1e18, mini2 = 1e18;
    int cnt1, cnt2;
    for (int i = 1; i <= n; ++i) {
        if (Find(i) == a[0]) {
            if (size[i] < mini1) {
                cnt1 = i;
                mini1 = size[i];
            }
        } else {
            if (size[i] < mini2) {
                cnt2 = i;
                mini2 = size[i];
            }
        }
    }
    v[cnt1].push_back(cnt2);
    v[cnt2].push_back(cnt1);
    sum = res = 0;
    dfs1(1, -1);
    dfs3(1, -1, 0);
    dfs2(1, -1, n);
    long long ans = 0;
    for (int i = 1; i <= n; ++i) {
        ans += size[i];
    }
    ans /= 2;
    cout << ans << endl;
}

 

引用\[1\]中提到了一种形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子。为了使cnt_white - cnt_black尽可能大,可以使用两次形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是链所代表的子的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C DP是什么问题?如何解决? 回答: CodeForces - 982C是一个形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值