[Bzoj3677] [Apio2014] Line (tree DP)

Source: Internet
Author: User

3677: [Apio2014] Line

Time Limit:10 Sec Memory limit:128 MB
submit:434 solved:270
[Submit] [Status] [Discuss]

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. Game
Starting with only 1 beads, 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 joins to a pair of beads connected by a red line (U, Countdown)
and change the red line 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. And at the end of the game, will get the game
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
There is the length of the connection, but you do not 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 Yan
Color F red or blue), so that the color scheme of the connection can be through the above mentioned two ways of connection
Operation, 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 Input

5 1 2 Ten 1 3  + 1 4  the 1 5  -

Sample Output

60





HINT

The data range satisfies the 1≤n≤200000.

Analysis: At first, the definition state F[I][0/1] indicates that the first point is not the center of the random tree DP can be, the result is only 10 points after the test

Reason: This problem is added in order, any moment to join the point is a unicom block. This way we are going to enumerate the root nodes and merge only the parent-child-grandchild. Then the complexity is O (n^2). We can optimize the change root, do not have to change root every time to enumerate, if each time only the current root node is not the root of the son, will be re-DP of only two points, the root of the complexity O (1) So the total complexity O (n) and then started with the vector what, Bzoj, Cena on a cool cold to the STL, forced to see someone else's code to change it QaqAC Code:

# include <iostream># include<cstdio>using namespacestd;Const intN = 3e5 + A;Const intINF =0x3f3f3f3f;intHead[n],dt,n,ans,f1[n],f2[n],g[n];structedge{intTo,nex,w;} Edge[n<<1];voidAddedge (intUintVintW) {edge[++DT] =(Edge) {v,head[u],w}; Head[u]=DT;}voidDfsintUintpre) {F1[u]= F2[u] =-inf;intT;  for(inti = Head[u];i;i =Edge[i].nex) {        if(edge[i].to = = pre)Continue;        DFS (EDGE[I].TO,U); T= G[edge[i].to] + edge[i].w-max (g[edge[i].to],g[edge[i].to] + f1[edge[i].to] +EDGE[I].W); if(T > F1[u]) f2[u] = F1[u],f1[u] =T; Else if(T > F2[u]) f2[u] =T; G[u]+ = max (G[edge[i].to],g[edge[i].to] + f1[edge[i].to] +EDGE[I].W); }}voidDfs (intUintpre) {ans= Max (Ans,g[u]);intS,t,c;  for(inti = Head[u];i;i =Edge[i].nex) {        if(edge[i].to = = pre)Continue; S= G[u]-MAX (g[edge[i].to],g[edge[i].to] + f1[edge[i].to] +EDGE[I].W); T= G[edge[i].to] + edge[i].w-max (g[edge[i].to],g[edge[i].to] + f1[edge[i].to] +EDGE[I].W); if(F1[u] = = t) c = f2[u];Elsec =F1[u]; G[edge[i].to]+ = max (s,s + C +EDGE[I].W); T= S + edge[i].w-max (s,s + C +EDGE[I].W); if(T > f1[edge[i].to]) f2[edge[i].to] = f1[edge[i].to],f1[edge[i].to] =T; Else if(T > f2[edge[i].to]) f2[edge[i].to] =T;    Dfs (Edge[i].to,u); }}intMain () {Freopen ("beads.in","R", stdin); Freopen ("Beads.out","W", stdout); scanf ("%d", &n);intx, Y, Z  for(inti =1; I < n;i++) {scanf (" %d%d%d",&x,&y,&z); Addedge (x, y, z);    Addedge (Y,X,Z); } DFS (1,-1);D FS (1,-1); printf ("%d\n", ans);    Fclose (stdin);    Fclose (stdout); return 0;}

[Bzoj3677] [Apio2014] Line (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.