HDU2196 computer (tree-shaped DP)

Source: Internet
Author: User

Like LightOJ1257, I used a tree to divide and write. In fact, this problem is a classic tree-shaped DP, feel this DP is not simple.

    • Dp[0][u] represents the maximum distance from a node in a subtree with u as the root
    • Dp[1][u] Represents the sub-distance between the nodes and U in a subtree with u as root

These two can be transferred once more by DFS through the son node. Obviously Dp[0][u] is a possible answer to u, that is, the furthest distance you go down , and the missing part is the furthest distance you go up :

    • Dp[2][u] Indicates the farthest distance you go up

For this transfer, in two cases, this is the case:

    1. DP[2][V] = max (Dp[0][u]+weight (u,v), Dp[2][u]+weight (u,v)) (V is the son of U and U goes down the farthest distance without going through V)
    2. DP[2][V] = max (Dp[1][u]+weight (u,v), Dp[2][u]+weight (u,v)) (V is the son of U and U goes down the farthest distance through V)

DFS can get it again.

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineMAXN 111116 structedge{7     intV,w,next;8}edge[maxn<<1];9 intNE,HEAD[MAXN];Ten voidAddedge (intUintVintW) { OneEdge[ne].v=v; edge[ne].w=W; AEdge[ne].next=head[u]; head[u]=ne++; - } - Long Longd[3][MAXN]; the intIDX[MAXN]; - voidDfs0 (intUintFA) { -     Long Longmx0=0, mx1=0; -      for(intI=head[u]; i!=-1; I=Edge[i].next) { +         intv=edge[i].v; -         if(V==FA)Continue; + Dfs0 (v,u); A         if(mx0<=d[0][V]+EDGE[I].W) mx1=mx0,mx0=d[0][v]+edge[i].w,idx[u]=v; at         Else if(mx1<d[0][V]+EDGE[I].W) mx1=d[0][v]+EDGE[I].W; -         Else if(mx1<d[1][V]+EDGE[I].W) mx1=d[1][v]+EDGE[I].W; -     } -d[0][u]=mx0; d[1][u]=MX1; - } - voidDFS1 (intUintFA) { in      for(intI=head[u]; i!=-1; I=Edge[i].next) { -         intv=edge[i].v; to         if(V==FA)Continue; +         if(IDX[U]==V) d[2][v]=max (d[1][u]+edge[i].w,d[2][u]+EDGE[I].W); -         Elsed[2][v]=max (d[0][u]+edge[i].w,d[2][u]+EDGE[I].W); the DFS1 (v,u); *     } $ }Panax Notoginseng intMain () { -     intn,a,b; the      while(~SCANF ("%d",&N)) { +Ne=0; Amemset (head,-1,sizeof(head)); the          for(intI=2; i<=n; ++i) { +scanf"%d%d",&a,&b); - Addedge (i,a,b); Addedge (a,i,b); $         } $memset (D,0,sizeof(d)); -Dfs0 (1,1); -DFS1 (1,1); the          for(intI=1; i<=n; ++i) { -printf"%lld\n", Max (d[0][i],d[2][i]));Wuyi         } the     } -     return 0; Wu}

HDU2196 computer (tree-shaped DP)

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.