HDU 5834 [Tree DP]

Source: Internet
Author: User
Tags cas

/*Test instructions: n points of a tree, both points and edges have the right value, when the first access to a point of time to gain benefits for the point of the weight of each passing an edge, loss of interest for the weight of the edge. What is the maximum benefit to be obtained from the first point? Input: Test sample group number of TNN the weight of each point n-1 a b c A and B is the label of the point, and C is the weight of the edge. Idea: Note that the topic only emphasizes that from a certain point, do not have to go back to that point. Consider a tree-shaped DP. Define a tree root first. And then for a point, we need to maintain two values of its subtree direction 1. From that point on, the end is going back to the maximum value of the point of interest of 2. From that point on, the maximum value of the benefit that ends up not having to go back to that point, and which son node is the last one to start from that point. 3. From this point on, it is not necessary to return to the second largest value of the interest at that point, and which son node is the last one to start from that point. The maximum value of the benefit that goes back to that point is first calculated, and then the node of its son is enumerated as the last node to go out, that is, the node does not return. The second and third values enumerate all the sons as the last departure and then sort records the first two. This is the first time that DFS has done the work. Then, for each point, maintain three values for the second DFS. 1. From the point of departure all directions (subtree direction and father direction) return to the maximum value of the point of interest. 2. From this point on all directions, do not have to go back to the maximum value of the point of interest, and save the last departure of the son node designator. 3. From that point on all directions, do not have to go back to the second big value of the point of interest, and save the last departure of the son node designator. The maintenance of the 1th value simply adds up the value of the parent direction that is affected by the subtree direction and the tree in which it is lost. For the second and third values, we can add the maximum of two values in the direction of the subtree plus the benefit of the father's direction back, and the father direction as the last point of departure for the benefit of the three value of the two maximum can be obtained. The reason for maintaining the second largest value is that when the final answer of the Father's node (that is, it can not come back) is exactly the point at which we asked for its son node, we can use the second-largest value to determine the benefit of the father direction of the son's node as the direction of the last visit. The last of all Ans[i][1] is the answer. Summary "1. When the tree-type DP is maintained, it is often necessary to maintain the secondary large or even the third largest value, which is due to the father's direction of the optimal problem is related to the son node decision. */#include<bits/stdc++.h>#defineN 100050using namespacestd;structedge{intID; Edge*Next; Long LongW;};structst{St (intALong LongBB) {ID=A; Ans=b; }    Long Longans; intID;};BOOLCMP (St A,st b) {returnA.ans>B.ans;}intEdnum;edge edges[n*2];edge*Adj[n];Long Longv[n],son[n][3],ans[n][3];intfa[n],id[n][3];inlinevoidAddedge (intAintBLong LongW) {Edge*tmp=&edges[ednum++]; TMP->w=W; TMP->id=b; TMP->next=Adj[a]; Adj[a]=tmp;}voidDfsintPOS) {son[pos][0]+=V[pos];  for(Edge *it=adj[pos];it;it=it->next) {        if(fa[pos]!=it->ID) {Fa[it->id]=POS; DFS (it-ID); if(son[it->id][0]-2*it->w>0) {son[pos][0]+=son[it->id][0]-2*it->W; } }} son[pos][1]=son[pos][0]; Vector<st>MV; Mv.push_back (St (0, son[pos][0])); Mv.push_back (St (0, son[pos][0]));  for(Edge *it=adj[pos];it;it=it->next) {        if(fa[it->id]==POS) {            if(son[it->id][1]-it->w>0){                Long Longtn=son[pos][0]+son[it->id][1]-it->W; if(son[it->id][0]-2*it->w>0) tn-=son[it->id][0]-2*it->W; if(tn>son[pos][0]) {Mv.push_back (ST (It-id,tn));    }}}} sort (Mv.begin (), Mv.end (), CMP); son[pos][1]=mv[0].ans; id[pos][1]=mv[0].id; son[pos][2]=mv[1].ans; id[pos][2]=mv[1].id;}voidDFS2 (intPOS) {    if(pos==1){         for(intI=0;i<3; i++) {Ans[pos][i]=Son[pos][i]; }    }     for(Edge *it=adj[pos];it;it=it->next) {        if(fa[it->id]==POS) {            Long Longtans=ans[pos][0]-2*it->W; if(son[it->id][0]-2*it->w>0) {Tans-=son[it->id][0]; Tans+=2*it->W; } Tans=Max (0ll,tans); Ans[it->id][0]=son[it->id][0]+tans; Vector<st>MV; Mv.push_back (St (0, ans[it->id][0])); Mv.push_back (St (0, ans[it->id][0]));  for(intI=1;i<3; i++) {Mv.push_back (st (Id[it->id][i],son[it->id][i]+tans)); }            if(id[pos][1]!=it->ID) {                Long Longtm=ans[pos][1]; TM+=son[it->id][0]; if(son[it->id][0]-2*it->w>0) {TM-=son[it->id][0]-2*it->W; } mv.push_back (St (id[pos][1],tm-it->W)); }            Else{                Long Longtm=ans[pos][2]; TM+=son[it->id][0]; if(son[it->id][0]-2*it->w>0) {TM-=son[it->id][0]-2*it->W; } mv.push_back (St (id[pos][1],tm-it->W));            } sort (Mv.begin (), Mv.end (), CMP); Ans[it->id][1]=mv[0].ans; Id[it->id][1]=mv[0].id; Ans[it->id][2]=mv[1].ans; Id[it->id][2]=mv[1].id; DFS2 (It-ID); }    }}intMain () {intT; scanf ("%d",&t); intcas=0;  while(t--) {CAs++; printf ("Case #%d:\n", CAs); intN; scanf ("%d",&N);  for(intI=0; i<=n;i++){             for(intj=0;j<3; j + +) {Son[i][j]=id[i][j]=ans[i][j]=0; } Adj[i]=NULL; Fa[i]=0; } ednum=0;  for(intI=1; i<=n;i++) scanf ("%lld", v+i);  for(intI=1; i<n;i++){            intb;Long LongW; scanf ("%d%d%lld",&a,&b,&W);            Addedge (A,B,W);        Addedge (B,A,W); } DFS (1); DFS2 (1);  for(intI=1; i<=n;i++) {printf ("%lld\n", ans[i][1]); }    }}

HDU 5834 [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.