[bzoj1999] The nucleus of the tree net

Source: Internet
Author: User

From the afternoon pit to the Internet. The original NOIP data is really weak, a number of places to write the result is also casual =

The most pit is the online some of the above-thought not thoughtful ... However, the NOIP data is too weak Noip the data is too weak Noip the data is too weak

The first step is to find the diameter, with two times BFS (or DFS,Linux under the system stack is quite large. ) resolved. Find one of the diameters, though Konjac Konjac won't prove it, but it seems to make sense.

To see the proof, you can read this: http://trinklee.blog.163.com/blog/static/238158060201411175015709/

The path on the diameter would have been n^2, but we found that the longer the path length (under the legal premise) of the same start/end point, the smaller the eccentricity, but the path has a length constraint, so it can be optimized with a monotone queue.

assuming that the diameter ends at x, y, and for one path (a, B), the point farthest from the path is either x and Y, or the path from A to B does not walk in the diameter of the node can be The farthest node to reach.

Assuming that X is closer to a, B is closer to Y, and if there is a node k that is not on the diameter that makes dist (k,a) >dist (x,a), then (x, y) is not a diameter ((k,a) (a,y) can be spelled in a longer path);

However, it is important to note that the above K nodes must meet the K to the nearest point of the path is the endpoint of the path ...

Otherwise, if the nearest point of K to the path is C, then dis (c,k) may also be offset ... The most intuitive example is that the given S is larger than the diameter length, so that the eccentric distance depends on K.

Of course Noip data enough water noip data enough water noip data enough water so ...

Monotone queue words Konjac Konjac got two monotone queue, one is incrementally store X to the distance of the node on the path, and the other decrements the point on the path to the other node (see above) the furthest distance ...

Of course, because the first monotone queue every time you delete the illegal team head, as long as directly in the queue at the end of the team, so you can actually do not open queues, as long as the record team head of the tail position is good =

1#include <cstdio>2#include <math.h>3#include <algorithm>4#include <iostream>5#include <cstring>6 using namespacestd;7 Const intmaxn=500233;8 structedge{9     intToo,pre,dis;Ten}e[maxn<<1]; One structzs{ A     intId,num; - }DLL[MAXN],Q[MAXN]; - intLAST[MAXN],NUM[MAXN],DL[MAXN],F[MAXN],P[MAXN]; the intdist[4][MAXN]; - inti,j,k,a,b; - intn,m,l,r,now,tot,posa,posb,s,c,ans,l1,r1; - BOOLU[MAXN],ARR[MAXN],CANT[MAXN]; +  -  +InlinevoidInsertintAintBintc) {e[++tot].too=b;e[tot].dis=c;e[tot].pre=last[a];last[a]=tot; Ae[++tot].too=a;e[tot].dis=c;e[tot].pre=last[b];last[b]=tot; at } - voidBFsintStintEdintID) { -     intL=0, r=1, now,i,j;dl[1]=st;u[st]=1;d ist[id][st]=0; -      while(l<R) { -now=dl[++l]; -          for(I=last[now];i;i=e[i].pre)if(!U[e[i].too]) { indl[++r]=e[i].too,dist[id][e[i].too]=dist[id][now]+e[i].dis,u[e[i].too]=1; -             if(id==3)if(dl[r]==ed) {l=r; Break; to             } +         } -     } thememset (U,0,sizeof(U)); *     if(id==3){ $arr[ed]=1; num[1]=ed;num[0]=1;Panax Notoginseng          for(i=r;i;i--)if(dist[2][dl[i]]+dist[3][dl[i]]==dist[2][ST]) num[++num[0]]=dl[i],arr[dl[i]]=1; -          the     } + } A voidBiiintx) { the     inti;u[x]=1; +      for(I=last[x];i;i=e[i].pre)if(!arr[e[i].too]&&!U[e[i].too]) { - bii (E[i].too); $F[x]=max (f[x],f[e[i].too]+E[i].dis); $     } - } - intMain () { thescanf"%d%d",&n,&s); -      for(i=1; i<n;i++) scanf ("%d%d%d",&a,&b,&c), insert (A,B,C);WuyiBFs1,0,1);p osa=0;d ist[1][0]=-1; for(i=1; i<=n;i++)if(dist[1][i]>dist[1][posa]) posa=i; theBFS (Posa,0,2);p osb=0;d ist[2][0]=-1; for(i=1; i<=n;i++)if(dist[2][i]>dist[2][POSB]) posb=i; -BFS (Posb,posa,3); Wu      for(i=1; i<=num[0];i++) Bii (Num[i]); -ans=1233333333; About      for(i=1; i<=num[0];i++) p[i]=dist[2][num[i]]; $L=1; r=0; l1=1; r1=0; -      for(i=1; i<=num[0];i++){ -          while(l<=r&&dist[2][num[i]]-dist[2][num[dll[l].id]]>s) cant[dll[l].id]=1, l++; -          while(L1<=r1&&cant[q[l1].id]) l1++; A          while(L1<=r1&&q[r1].num<=f[num[i]]) r1--; +Q[++r1].num=f[num[i]];q[r1].id=i; theDll[++r].num=p[i];d ll[r].id=i; -Ans=min (Ans,max (Max (dll[l].num,dist[3][num[i]), q[l1].num)); $     } theprintf"%d\n", ans); the     return 0; the}
View Code

Because I changed it many times, there are many useless arrays in the program (covering the face)

Why is the Noip problem on the B station so insane? Qaq

1999: [Kernel of Noip2007]core tree net

Time Limit:10 Sec Memory limit:64 MB

Description

Set t= (V, E, W) is a non-circle and connected undirected graph (also known as a root tree), each edge to have a positive integer right, we call T a tree net (treebetwork), wherein the v,e respectively represents a set of nodes and edges, W represents the set of each edge length, and set T has n nodes.

Path: Any two nodes in the tree network, A and B, have a unique simple path, with D (A, b) as the length of the path to a, and the end of the endpoint, which is the sum of the lengths of each side of the path. We call D (A, b) the distance between A and b two nodes.

D (V, P) =min{d (V, u), U is the node on path P}.

Tree Net Diameter: the longest path in the tree network becomes the diameter of the tree net. For a given tree mesh t, the diameter is not necessarily unique, but it can be proved that: the midpoint of each diameter (not necessarily exactly a node, may be in the interior of an edge) is unique, we call this point is the center of the Tree network.

Eccentricity ECC (F): The distance from the farthest node in the tree mesh t to the path F, i.e.

ECC (f) =max{d (V, f), v∈v}

Task: For a given tree net t= (V, E, W) and nonnegative integer s, find a path F, which is a path on a certain diameter (the path both ends are nodes in the tree network), whose length does not exceed s (can be equal to s), so that the eccentricity of ECC (F) is minimized. We call this path the core (core) of the tree mesh t= (V, E, W). If necessary, F can degenerate into a node. In general, under the above definition, the nucleus is not necessarily the only one, but the minimum eccentricity is unique.

The following figure shows an example of a tree network. In the figure, A-B and a-c are two diameters and each length is 20. Point W is the center of the tree net, and the EF side is 5 in length. If s=11 is specified, the kernel of the tree network is the path DEFG (which can also be taken as Path Def) with an eccentric distance of 8. If you specify S=0 (or S=1, s=2), the core of the tree mesh is node F, and the eccentricity is 12.

Input contains n rows: line 1th, two positive integers n and S, separated by a space. where n is the number of nodes in the tree network, S is the upper bound of the length of the kernel of the tree network. Set the node number in order 1, 2, ..., N. From line 2nd to nth row, each row gives 3 positive integers separated by spaces, which in turn represent the two endpoint numbers and lengths for each edge. For example, "2 4 7" indicates the length of the edge of the Connection node 2 and 4 is 7. The data given are correct and do not have to be tested. Output has only a non-negative integer, which is the minimum eccentricity in the specified sense. Sample Input5 2
1 2 5
2 3 2
2 4 4
2 5 3
Sample Output5HINT

For 70% of data, n<=200000
For 100% data: n<=500000, s<2^31, ownership value <500

==============================================
It seems spoj on the enhanced version of the data ...

[bzoj1999] The nucleus of the tree net

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.