Hdu3534, a classic tree-like DP

Source: Internet
Author: User

Calculate the diameter (longest path) of a tree and the number of diameter.

The simple DP can only find the longest path starting from a certain point, but this longest path is not necessarily the diameter of the tree. It is easy to start from the weak first, always wa

Until I read the question of a Daniel...

According to the idea of the Daniel, we should consider the composition of the diameter:

Scenario 1: The longest path generated by a leaf node is directly composed

Case 2: It is composed of two long paths generated by a node with multiple sons. In this case, the two long paths can be divided into two conditions: whether the length of the two long paths is equal or not.

Therefore, in DP, we need to record the longest and second long paths generated by each node and their quantity. The statistics on the quantity is also very troublesome.

For details, see the code:

#include<stdio.h>#include<iostream>#include<stdlib.h>#include<math.h>#include<ctype.h>#include<algorithm>#include<string>#include<string.h>#include<queue>#define mod 998244353#define MAX 100000000using namespace std;int t,n,m,p,k,tt,f;int x;int head[10010];typedef struct Node{    int en;    int value;    int next;}node;node edge[20010];typedef struct DPnode{    int dp1,dp2,len,nn;    int n1,n2;}DP;DP dp[10010];void ini(){    int x,y,z;    for(int i=1;i<=n-1;i++)    {        scanf("%d%d%d",&x,&y,&k);        edge[2*i-1].en=y;        edge[2*i-1].next=head[x];        edge[2*i-1].value=k;        head[x]=2*i-1;        edge[2*i].en=x;        edge[2*i].next=head[y];        edge[2*i].value=k;        head[y]=2*i;    }}void dfs(int s,int p){    dp[s].dp1=dp[s].dp2=dp[s].len=dp[s].n1=dp[s].n2=dp[s].nn=0;    int leaf=1;    for(int i=head[s];i;i=edge[i].next)    {        int q=edge[i].en;        if(q==p)            continue;        leaf=0;        dfs(q,s);        int tmp=dp[q].dp1+edge[i].value;        if(tmp>dp[s].dp1)        {            dp[s].dp2=dp[s].dp1;            dp[s].n2=dp[s].n1;            dp[s].dp1=tmp;            dp[s].n1=dp[q].n1;        }        else if(tmp==dp[s].dp1)        {            dp[s].n1+=dp[q].n1;        }        else if(tmp>dp[s].dp2)        {            dp[s].dp2=tmp;            dp[s].n2=dp[q].n1;        }        else if(tmp==dp[s].dp2)        {            dp[s].n2+=dp[q].n1;        }    }    if(leaf)    {        dp[s].n1=1;dp[s].nn=1;        dp[s].len=0;        dp[s].dp1=0;        return;    }    int c1=0,c2=0;    for(int i=head[s];i;i=edge[i].next)    {        int q=edge[i].en;        if(q==p)            continue;        int tmp=dp[q].dp1+edge[i].value;        if(tmp==dp[s].dp1)            c1++;        else if(tmp==dp[s].dp2&&dp[s].dp2)            c2++;    }    if(c1>1)    {        dp[s].len=dp[s].dp1*2;        int sum=0;        for(int i=head[s];i;i=edge[i].next)        {            int q=edge[i].en;            if(q==p)                continue;            if(dp[q].dp1+edge[i].value==dp[s].dp1)            {                dp[s].nn+=sum*dp[q].n1;                sum+=dp[q].n1;            }        }    }    else if(c2>0)    {        dp[s].len=dp[s].dp1+dp[s].dp2;        for(int i=head[s];i;i=edge[i].next)        {            int q=edge[i].en;            if(q==p)                continue;            if(dp[q].dp1+edge[i].value==dp[s].dp2)            {                dp[s].nn+=dp[s].n1*dp[q].n1;            }        }    }    else    {        dp[s].len=dp[s].dp1;        dp[s].nn=dp[s].n1;    }    return ;}void solve(){    int ans=0;    int num=0;    for(int i=1;i<=n;i++)    {        if(dp[i].len>ans)        {            ans=dp[i].len;            num=dp[i].nn;        }        else if(dp[i].len==ans)        {            num+=dp[i].nn;        }    }    printf("%d %d\n",ans,num);}int main(){    while(scanf("%d",&n)!=EOF)    {        memset(head,0,sizeof(head));        ini();        dfs(1,0);        solve();    }    return 0;}

 

Hdu3534, a classic tree-like DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.