[Tree DP] HDU 2196-computer

Source: Internet
Author: User

Main topic

Finding the longest distance from each point in the tree to any point

Ideas

There are two kinds of distance, one is through the Father node, and one is the subtree.
Define DP[X][0/1] represents the maximum and minor distance from X to leaf nodes
DP[X][2] indicates the longest path through the father's node
DP[X][2] can be the first to go to the Father's node, and then go to the father's other sons, may be to go to the Father node and then go to Father's father
At this time, if the father's furthest leaf node passes through the point, it is updated with the second large value, otherwise the maximum value is updated

#include <cstdio> #include <cstring> #include <iostream> #define MAX (b) ((a) > (c)? (    A):(B) typedef long LONG ll;const int maxn=10000+5;inline int Read () {int X=0;char c=getchar (); while (' 0 ' >c| |    C> ' 9 ') C=getchar ();    while (' 0 ' <=c&&c<= ' 9 ') x= (x<<1) + (x<<3) +c-48,c=getchar (); return x;} struct edge{int to,nxt,data;}    E[maxn<<1];int n,head[maxn],cnt,dp[maxn][3];inline void Insert (int x,int y,int z) {e[++cnt].to=y;e[cnt].data=z; e[cnt].nxt=head[x];head[x]=cnt;}    #define Y e[i].tovoid Dfs1 (int x) {dp[x][0]=dp[x][1]=dp[x][2]=0;        for (int i=head[x];i;i=e[i].nxt) {Dfs1 (y);            if (Dp[y][0]+e[i].data>dp[x][0]) {dp[x][1]=dp[x][0]; Dp[x][0]=dp[y][0]+e[i].data;//dp[x][0] represents the longest distance from x to the leaf node in the subtree} else Dp[x][1]=max (dp[x][1],dp[y][0]+e[i].data);//dp[            X][1] Indicates the second long distance}}void Dfs2 (int x) {for (int i=head[x];i;i=e[i].nxt) {if (dp[x][0]==dp[y][0]+e[i].data) Dp[y][2]=max (Dp[x][1],dp[x][2])        +e[i].data;        else Dp[y][2]=max (dp[x][0],dp[x][2]) +e[i].data;    DFS2 (y); }//dp[x][2] represents the longest path through the parent node, at this point can go to the father's node to the father's other sons, but also from the father to Father//if the parent node to the maximum value of the leaf node through Y, then use the second large value update, or use the maximum value} #undef yint Main (    ) {int x, y;        while (scanf ("%d", &n)!=eof) {cnt=0;        memset (head,0,sizeof (head));        for (int i=2;i<=n;++i) X=read (), Y=read (), Insert (x,i,y);        DFS1 (1);D FS2 (1);    for (int i=1;i<=n;++i) printf ("%d\n", Max (dp[i][0],dp[i][2])); } return 0;}

[Tree Dp]hdu 2196-computer

Related Article

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.