hdu2196 Tree DP

Source: Internet
Author: User

Idea: For a tree, for any node, its maximum value can come from a subtree rooted in it, or it can come from its father.
It is therefore necessary to consider the situation in two directions. Therefore, 2 directions must be considered.
Solution: Dp[i][0] Save subtree value, Dp[i][1] Save Father node Direction +dis[i,root];
You can save the maximum value of any subtree by first DFS, while preserving the direction of the maximum at any point while maintaining his second largest value.
Then the second time Dfs, for node u, if its child is not it to take the maximum path on the point, then the child's value dp[i][1] is
Max (dp[root][1], dp[root][0]) + dis[i,root]; Dp[i][1] is the second largest value of the Father node + dis[i,root];
Then the second largest value is dp[i][1];

#include <cstdio>#include<cstring>#include<iostream>using namespacestd;Const intMAXN =10010;structnode{intto ; intv; intNext;} EDGE[MAXN*3];intPre[maxn],index,vis[maxn],n;intdp[maxn][2],WAY[MAXN],SV[MAXN];//sv[] On the other side of the way[that is not a subtree] indicates where the maximum value of the current point comes from//Dp[][0] Indicates the maximum value of the subtree direction dp[][1] represents the maximum value in the other directionintMaxintXinty) {    returnX>y?x:y;}intMinintXinty) {    returnX<y?x:y;}voidAddintXintYintz) {edge[index].to=y; EDGE[INDEX].V=Z; Edge[index].next=Pre[x]; PRE[X]= index++;}voidDFS1 (introot) {Vis[root]=1; inti; int Set=Root;  for(I=pre[root]; i!=-1; I=Edge[i].next) {        intt =edge[i].to; if(!Vis[t])            {DFS1 (t); if(dp[root][0] > dp[t][0] +edge[i].v) {Sv[root]= Max (Sv[root], dp[t][0] +edge[i].v); }            Else {                Set=T; Sv[root]= dp[root][0]; dp[root][0] = dp[t][0] +edge[i].v; }}} Way[root]=Set;}voidDFS2 (introot) {Vis[root]=1; inti;  for(I=pre[root]; i!=-1; I=Edge[i].next) {        intt =edge[i].to; if(!Vis[t]) {            if(Way[root]! =t) {dp[t][1] = max (dp[root][0], dp[root][1]) +edge[i].v; Sv[t]= dp[t][1]; }            Else{dp[t][1] = Sv[root] +edge[i].v; Sv[t]= dp[t][1];        } DFS2 (t); }    }}intMain () {inti;  while(~SCANF ("%d",&N)) {index=1; memset (SV,0,sizeof(SV)); Memset (DP,0,sizeof(DP)); memset (PRE,-1,sizeof(pre));  for(i=2; i<=n; ++i) {            intx,v; scanf ("%d%d", &x, &v);            Add (i, X, V);        Add (x, I, v); } memset (Vis,0,sizeof(VIS)); DFS1 (1); /*for (i=1; i<=n; i++) {printf ("%d", sv[i]); } cout<<endl;*/memset (Vis,0,sizeof(VIS)); DFS2 (1);  for(i=1; i<=n; i++) {printf ("%d\n", Max (dp[i][0],dp[i][1])); }    }}/*1 of 181 , one by one*/

hdu2196 Tree 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.