树的直径(两次DFS + 树形DP)

方式一:两遍dfs

注此方法只适用于非负权边

从树上任一点a开始dfs,找到距离a距离最远的点b,再从b找到距离b点最远的点c。那么b - > c即为树的直径。

如果b点为某一条直径的一个端点,那么树上距离b最远的点c一定是该直径的另一端点。 所以我们要证的就是b是否为树上某一条直径的一个端点

证明如下:

设be为树上一条直径

1. 当bc与de不相交时,如下图:

在这里插入图片描述

我们知道距离a最远的点是b,由于树是联通的,所以a点一定可以通过某个点到达e点。因此我们就可以看出 x 1 > = x 2 + x 3 x1 >= x2 + x3 x1>=x2+x3
由于所有边权都是非负的,所以 x 1 + x 2 > = x 1 − x 2 > = x 3 x1 + x2 >= x1 - x2 >= x3 x1+x2>=x1x2>=x3可以证明出b也是距离d最远的一个点,可以证明出b为直径的一个端点。


2.当bc与de相交时,如下图:

在这里插入图片描述

我们可以看出距离 a 最远的点是 b ,那么可以推出:
x 1 > = x 2 x1 >= x2 x1>=x2
可以推出b也是距离d点最远的点之一,所以b点为一条直径端点。
综上两种情况证明出b一定为某一直径端点。再找出距离b最远的点c那么bc就是树的直径。


接下来就是非常简洁的代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <cstdio>

#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, char> PIC;
typedef pair<char, int> PCI;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
const int N = 100010, M = N * 2, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int h[N], e[M], ne[M], w[M], idx;
int dis[N];
int n, c = 1;

void add(int a, int b, int c) // 邻接表存树
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

//爆搜,最后找到距离u最大的点c,可以用一个bool数组代替father
void dfs(int u, int father) 
{
    for (int i = h[u]; ~i; i = ne[i])
    {
        int j = e[i];
        if (j == father)
            continue;
        dis[j] = dis[u] + w[i];
        if (dis[j] > dis[c])
            c = j;
        dfs(j, u);
    }
}

void solve()
{
    cin >> n;
    memset(h, -1, sizeof h);
    for (int i = 0; i < n - 1; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c), add(b, a, c);
    }
    dfs(1, -1); //找到距离1号点最远的点c
    dis[c] = 0;
    dfs(c, -1); //找到距离c最远的点,这个c和最后结果中dis[c]中的c不是同一个。
    cout << dis[c] << endl;
}

int main()
{

    solve();
    return 0;
}

方式二 树形DP(主推)

此方法可以处理负权边。
在这里插入图片描述
对于每棵子树来说,例如a子树,以a为根每个节点作为子树的根向下,所能延伸的最远距离d1 ,和次远距离d2 ,那么直径就是所有 d1 + d2 的最大值。用子节点更新父节点,最后求出root的向下延伸的d1和d2,二者之和即为最大值。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <cstdio>

#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<int, char> PIC;
typedef pair<char, int> PCI;
typedef pair<LL, LL> PLL;
typedef unsigned long long ULL;
const int N = 10010, mod = 1e9 + 7, INF = 0x3f3f3f3f;
int h[N], w[N * 2], ne[N * 2], e[N * 2], idx;
int n;
int ans = 0;

void add(int a, int b, int c)
{
    e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}

//返回以u为根的子树向下延伸的最大长度
int dfs(int u, int father)
{
    int d1 = 0, d2 = 0;
    for (int i = h[u]; ~i; i = ne[i])
    {
        int j = e[i];
        if (j == father)
            continue;
        int d = dfs(j, u) + w[i];
        if (d >= d1)
            d2 = d1, d1 = d;
        else if (d > d2)
            d2 = d;
    }
    ans = max(ans, d1 + d2); // ans 即为答案
    return d1;
}

void solve()
{
    cin >> n;
    memset(h, -1, sizeof h);
    for (int i = 0; i < n - 1; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c), add(b, a, c);
    }
    dfs(1, -1);
    cout << ans << endl;
}

int main()
{
    solve();
    return 0;
}

OVER

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值