• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
树形DP,树的直径

转载地址:http://www.cnblogs.com/celia01/archive/2012/07/30/2615842.html

树的直径:树中距离最远的两点间的距离。

下面说几道题:

hdu 2196:对于树上(双向边)的每一个节点求出与其距离最远的点的距离。

这个主要用的思想是两次dfs:一次dfs将无向图转化为有跟树(所 以一开是一定要是建双向边,不然很可能wa或者tle,记录过程中可以开数组记入父亲节点,也可以在dfs递推过程中以栈的形式记录)求出每个跟节点到其 所有的叶子节点的最远距离f[i]和g[i]。再一次dfs求出能够由父亲节点转化得到的最大距离h[i],求h[i]的过程中就有可能用到f[i]和 g[i]了,因为如果i节点在其父亲j节点的最远距离f[j]中,那么f[i]就只能由g[j]或者h[j]得到,不然就由f[j]或者h[j]得到,具 体可能说的不是特别清。两个dfs综合起来的复杂度只有O(E)

poj 1985:求树的直径。个人觉得大概有3种方法。

第一种是如上题hdu2196的写法,求出每个点的最远距离最后取最大值,这样不会增加太多的复杂度,因为每次dfs也都只是O(E)的复杂度。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 40005;
int f[maxn], g[maxn], h[maxn], longest[maxn];
vector<int> son[maxn], w[maxn];

int dfs(int root, int pre){
    int i, j;
    int est = 0, esti=-1, er=0;
    if(f[root]!=-1) return f[root];
    if(son[root].empty()) return f[root] = 0;
    for(i=0;i<son[root].size();i++){
        if(pre!=son[root][i]){
            if(dfs(son[root][i],root)+w[root][i]>est){
                est = f[son[root][i]]+w[root][i];
                esti = i;
            }
        }
    }
    longest[root] = esti;
    for(i=0;i<son[root].size();i++){
        if(pre!=son[root][i]){
            if(f[son[root][i]]+w[root][i]>er&&son[root][i]!=longest[root]){
                er = f[son[root][i]]+w[root][i];
            }
        }
    }
    g[root] = er;
    return f[root] = est;
}

void dfs1(int root, int pre){
    int i, j;
    for(i=0;i<son[root].size();i++){
        if(pre!=son[root][i]){
            if(i!=longest[root]){
                h[son[root][i]] = max(h[root],f[root])+w[root][i];
            }
            else{
                h[son[root][i]] = max(h[root],g[root])+w[root][i];
            }
            dfs1(son[root][i],root);
        }
    }
}

void Init(int n){
    int i;
    for(i=0;i<=n;i++){
        f[i] = g[i] = h[i] = longest[i] = -1;
        son[i].clear();
        w[i].clear();
    }
}
int main(){
    int i, j, k, n, m, ans;
    int x1, x2, l;
    char opt;
    while(~scanf("%d%d",&n,&m)){
        Init(n);
        for(i=0;i<m;i++){
            scanf("%d%d%d",&x1,&x2,&l);
            scanf(" %c",&opt);
            son[x1].push_back(x2);w[x1].push_back(l);
            son[x2].push_back(x1);w[x2].push_back(l);
        }
        for(i=1;i<=n;i++){
            if(f[i]==-1){
                f[i] = dfs(i,-1);
                h[i] = 0; dfs1(i,-1);
            }
        }
        ans = 0;
        for(i=1;i<=n;i++){
            ans = max(ans,max(f[i],h[i]));
        }
        printf("%d\n",ans);
    }
    return 0;
}

 第二种是利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点。这样就可以首先任取一个点,bfs求出离其最远的点,在用同样的方法求出离这个叶子节点最远的点,此时两点间的距离就是树的直径。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 40005;
vector<int> son[maxn], w[maxn];
bool vis[maxn], viss[maxn];
int f[maxn];
int bfs(int root){
    int i, j, k;
    int ans = root, maxx = 0;
    queue<int> q;
    memset(vis,0,sizeof(vis));
    memset(f,0,sizeof(f));
    q.push(root);
    vis[root] = 1;f[root] = 0;viss[root] = 1;
    while(!q.empty()){
        root = q.front();
        q.pop();
        for(i=0;i<son[root].size();i++){
            if(vis[son[root][i]]==0){
                q.push(son[root][i]);
                vis[son[root][i]] = 1;viss[son[root][i]] = 1;
                f[son[root][i]] = f[root]+w[root][i];
                if(maxx<f[son[root][i]]){
                    maxx = f[son[root][i]];
                    ans = son[root][i];
                }
            }
        }
    }
    return ans;
}
int solve(int root){
    int  u, v;
    u = bfs(root);
    v = bfs(u);
    return f[v];
}
int main(){
    int i, j, k, n, m;
    int x1, x2, l, u;
    int res;
    char opt;
    while(~scanf("%d%d",&n,&m)){
        for(i=0;i<=n;i++){
            son[i].clear();
            w[i].clear();
        }
        for(i=0;i<m;i++){
            scanf("%d%d%d",&x1,&x2,&l);
            scanf(" %c",&opt);
            son[x1].push_back(x2);w[x1].push_back(l);
            son[x2].push_back(x1);w[x2].push_back(l);
        }
        res = 0;
        memset(viss,0,sizeof(vis));
        for(i=1;i<=n;i++){
            if(viss[i]==0){
                res = max(res,solve(i));
            }
        }
        printf("%d\n",res);
    }
    return 0;
}

 

ps:搜索也可以用dfs的方法搜,不过个人觉得bfs虽然更长,但比较好写,不容易出错。

第三种方法应该也可以算是树的直径的一个性质了吧,树的直径的长度一定会是某个点的最长距离f[i]与次长距离g[i]之和。最后求出max{f[i]+g[i]}就可以了,用到方法1中的第一个dfs就行了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 40005;
int f[maxn], g[maxn], longest[maxn];
vector<int> son[maxn], w[maxn];
int dfs(int root, int pre){
    int i, j;
    int est = 0, esti=-1, er=0;
    if(f[root]!=-1) return f[root];
    if(son[root].empty()) return f[root] = 0;
    for(i=0;i<son[root].size();i++){
        if(pre!=son[root][i]){
            if(dfs(son[root][i],root)+w[root][i]>est){
                est = f[son[root][i]]+w[root][i];
                esti = i;
            }
        }
    }
    longest[root] = esti;
    for(i=0;i<son[root].size();i++){
        if(pre!=son[root][i]){
            if(f[son[root][i]]+w[root][i]>er&&son[root][i]!=longest[root]){
                er = f[son[root][i]]+w[root][i];
            }
        }
    }
    g[root] = er;
    return f[root] = est;
}

void Init(int n){
    int i;
    for(i=0;i<=n;i++){
        f[i] = g[i] = longest[i] = -1;
        son[i].clear();
        w[i].clear();
    }
}
int main(){
    int i, j, k, n, m, ans;
    int x1, x2, l;
    char opt;
    while(~scanf("%d%d",&n,&m)){
        Init(n);
        for(i=0;i<m;i++){
            scanf("%d%d%d",&x1,&x2,&l);
            scanf(" %c",&opt);
            son[x1].push_back(x2);w[x1].push_back(l);
            son[x2].push_back(x1);w[x2].push_back(l);
        }
        for(i=1;i<=n;i++){
            if(f[i]==-1){
                f[i] = dfs(i,-1);
            }
        }
        ans = 0;
        for(i=1;i<=n;i++){
            ans = max(ans,f[i]+g[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

 树的直径应该就是以上几种方法了吧,不过从poj1985的三种方法可以得出用搜索的方法求某个点的最远的点的距离了,就是先对任意一个点求距离其最远的 顶点,最后可以得到一条树的直径的两个端点,以这两个端点开始去遍历整棵树,两个端点到每个点的距离较大值就会是这个点在树上能够走的最远距离。手画了几 个sample,觉得如果树有多条直径,这个结论也应该是正确的。

posted on 2016-07-11 10:39  dwtfukgv  阅读(292)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3