"BZOJ3677" [Apio2014] line change root DP

Source: Internet
Author: User

"BZOJ3677" [Apio2014] Line Description

During the time of Leonardo da Vinci, there was a popular childhood game called "Line of Lines". As expected, play this game only need beads and lines, beads from 1 to the courtesy number, the line is divided into red and blue. When the game starts, there are only 1 beads, and the next new bead can only be joined by the following two ways:
1. Append (W, Countdown):-A new bead W and an existing bead countdown connection, connecting using the red line.
2. Insert (W,U,V):-A new bead W is added to a pair of beads connected by a red line (U, Countdown), and the red line is changed to blue. That is, the red line that connects the original U to 1 becomes the Blue Line of U connected to W and W to v.
Each line has a length regardless of the red or blue line. At the end of the game, you will get the game's final score: the sum of the lengths of all the blue lines.
Now there is a final structure for this game: you will get the connection between all the beads and the length of all the wires, but you don't know what the color of each line is.
You now need to find the maximum score under this structure, that is to say: you need to give each line a color f red or blue, so that the color scheme of this connection can be obtained by the two connection methods mentioned above, and the game scored the most. In this scenario you only need to output the maximum score.

Input

The first line is a positive integer n, which represents the number of beads and the bead number is 1 manipulate N.
Next N-l line, three positive integers per line Ai,bi (l≤ai10000), indicates that a line with a CI length is connected to bead AI and bead bi.

Output

Outputs an integer that is the maximum score for the game.

Sample Input5
1 2 10
1 3 40
1 4 15
1 5Sample Output60
HINT

The data range satisfies the 1≤n≤200000.

The first thought of a non-root DP, the result is wrong ...

First select a point when the root, then all the blue line must be the father-son-sun such. So we make g[x] that the current point is not the maximum score of ' child ', f[x] indicates that the current point is ' child ' of the maximum score, the tree DP can be.

But how do we change roots? We need to maintain the maximum and secondary values of f and then we can do it.

#include <cstdio> #include <cstring> #include <iostream>using namespace Std;const int Maxn=200010;int N,cnt,ans;int to[maxn<<1],next[maxn<<1],val[maxn<<1],head[maxn],f1[maxn],g[maxn],f2[maxn]; inline void Add (int a,int b,int c) {to[cnt]=b,val[cnt]=c,next[cnt]=head[a],head[a]=cnt++;} inline Int rd () {int Ret=0,f=1;char gc=getchar (); while (gc< ' 0 ' | | Gc> ' 9 ') {if (gc== '-') F=-f;gc=getchar ();} while (gc>= ' 0 ' &&gc<= ' 9 ') ret=ret*10+ (gc^ ' 0 '), Gc=getchar (); return ret*f;} void dfs1 (int x,int fa) {f1[x]=f2[x]=-1<<30;for (int y,i=head[x];i!=-1;i=next[i]) if (TO[I]!=FA) {y=to[i],dfs1 (Y, x); int Fy=g[y]+val[i]-max (g[y],g[y]+f1[y]+val[i]), if (Fy>f1[x]) F2[x]=f1[x],f1[x]=fy;elseif (fy>f2[x]) f2[x]= Fy;g[x]+=max (G[y],g[y]+f1[y]+val[i]);}} void dfs2 (int x,int fa) {Ans=max (ans,g[x]); for (int y,i=head[x];i!=-1;i=next[i]) if (TO[I]!=FA) {Y=to[i];int Gx=g[x]-max (G[y],g[y]+f1[y]+val[i]), Fx;if (F1[x]==g[y]+val[i]-max (G[y],g[y]+f1[y]+val[i])) Fx=f2[x];elsefx=f1[x];g[y]+=max ( gx,gx+Fx+val[i]); Fx=gx+val[i]-max (Gx,gx+fx+val[i]); if (Fx>f1[y]) F2[y]=f1[y],f1[y]=fx;elseif (fx>f2[y]) f2[y]=fx; DFS2 (y,x);}} int main () {n=rd (); int I,a,b,c;memset (head,-1,sizeof (head)), for (i=1;i<n;i++) a=rd (), B=rd (), C=rd (), add (A,b,c), Add (b,a,c);d FS1 (1,0), DFS2 (1,0);p rintf ("%d", ans); return 0;}

"BZOJ3677" [Apio2014] line change root 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.