hdu3534 樹形dp(求樹中兩點之間的最大距離),hdu3534dp

來源:互聯網
上載者:User

hdu3534 樹形dp(求樹中兩點之間的最大距離),hdu3534dp

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



Problem DescriptionIn 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? 
InputThere 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.

 
OutputFor each test case, output the length of longest path and its number in one line. 
Sample Input
41 2 1002 3 502 4 5041 2 1002 3 503 4 50
 
Sample Output
150 2200 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 DescriptionIn 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? 
InputThere 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.

 
OutputFor each test case, output the length of longest path and its number in one line. 
Sample Input
41 2 1002 3 502 4 5041 2 1002 3 503 4 50
 
Sample Output
150 2200 1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.