hdu3534 树形dp(求树中两点之间的最大距离)

25 篇文章 0 订阅

http://acm.hdu.edu.cn/showproblem.php?pid=3534



Problem Description
In the Data structure class of HEU, the teacher asks one problem: How to find the longest path of one tree and the number of such longest path?
 

Input
There are several test cases. The first line of each case contains only one integer N, means there are N nodes in the tree. N-1 lines follow, each line has three integers w,v and len, indicate that there is one edge between node w and v., and the length of the edge is len.

 

Output
For each test case, output the length of longest path and its number in one line.
 

Sample Input
  
  
4 1 2 100 2 3 50 2 4 50 4 1 2 100 2 3 50 3 4 50
 

Sample Output
  
  
150 2 200 1

/**
hdu 3534  树形dp
题目大意:求给定的树中两点之间的最大长度和这种路的条数
解题思路:首先统计出结点到叶子结点的最长距离和次长距离。然后找寻经过这个点的,
           在这个为根结点的子树中的最长路径个数目。
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100010;

struct note
{
    int v,w,next;
} edge[maxn*2];

int head[maxn],ip;
int n;
int maxx[maxn];///该节点往下到叶子结点的最大距离
int smaxx[maxn];/// 次大距离
int num_maxx[maxn];///最大距离的个数
int num_smaxx[maxn];///次大距离的个数
int path[maxn];///该结点为根的子树中,包含该结点的最长路径长度
int num[maxn];///最长路径的个数

void init()
{
    memset(head,-1,sizeof(head));
    ip=0;
}

void addedge(int u,int v,int w)
{
    edge[ip].v=v,edge[ip].w=w,edge[ip].next=head[u],head[u]=ip++;
}

void dfs(int u,int pre)
{
    maxx[u]=smaxx[u]=0;
    num_maxx[u]=num_smaxx[u]=0;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        int w=edge[i].w;
        if(v==pre)continue;
        dfs(v,u);
        if(maxx[v]+w>maxx[u])
        {
            smaxx[u]=maxx[u];///随之更新次长信息
            num_smaxx[u]=num_maxx[u];
            maxx[u]=maxx[v]+w;
            num_maxx[u]=num_maxx[v];
        }
        else if(maxx[v]+w==maxx[u])
        {
            num_maxx[u]+=num_maxx[v];
        }
        else if(maxx[v]+w>smaxx[u])
        {
            smaxx[u]=maxx[v]+w;
            num_smaxx[u]=num_maxx[v];
        }
        else if(maxx[v]+w==smaxx[u])
        {
            num_smaxx[u]+=num_maxx[v];
        }
    }
    if(num_maxx[u]==0)///叶子节点
    {
        maxx[u]=smaxx[u]=0;
        num_maxx[u]=num_smaxx[u]=1;
        path[u]=0;
        num[u]=1;
        return;
    }
    int c1=0,c2=0;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        int w=edge[i].w;
        if(v==pre)continue;
        if(maxx[v]+w==maxx[u]) c1++;
        else if(maxx[v]+w==smaxx[u])c2++;
    }
    path[u]=0;
    num[u]=0;
    if(c1>=2)///最长+最长
    {
        int tmp=0;
        path[u]=maxx[u]*2;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v;
            int w=edge[i].w;
            if(v==pre)continue;
            if(maxx[u]==maxx[v]+w)
            {
                num[u]+=tmp*num_maxx[v];
                tmp+=num_maxx[v];
            }
        }
    }
    else if(c1==1&&c2>=1)///最长+次长
    {
        path[u]=maxx[u]+smaxx[u];
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v;
            int w=edge[i].w;
            if(v==pre)continue;
            if(maxx[u]==maxx[v]+w)
            {
               num[u]+= num_maxx[v]*num_smaxx[u];
               break;
            }
        }
    }
    else ///最长
    {
        path[u]=maxx[u];
        num[u]=num_maxx[u];
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        init();
        for(int i=1; i<n; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        dfs(1,-1);
        int ans1=0,ans2=0;
        for(int i=1;i<=n;i++)
        {
            if(path[i]>ans1)
            {
                ans1=path[i];
                ans2=num[i];
            }
            else if(path[i]==ans1)
            {
                ans2+=num[i];
            }
        }
        printf("%d %d\n",ans1,ans2);
    }
    return 0;
}


Problem Description
In the Data structure class of HEU, the teacher asks one problem: How to find the longest path of one tree and the number of such longest path?
 

Input
There are several test cases. The first line of each case contains only one integer N, means there are N nodes in the tree. N-1 lines follow, each line has three integers w,v and len, indicate that there is one edge between node w and v., and the length of the edge is len.

 

Output
For each test case, output the length of longest path and its number in one line.
 

Sample Input
   
   
4 1 2 100 2 3 50 2 4 50 4 1 2 100 2 3 50 3 4 50
 

Sample Output
   
   
150 2 200 1
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值