BZOJ2282[SDOI2011] Fire
Test instructions
Find a path on the tree that minimizes the distance from the endpoint to this path.
Exercises
A pit, that is, this path can contain no one edge and only one node. It is therefore possible to prove that the path is in the diameter of the tree, storing all the sides of the tree's diameter in a sequence, and the maximum distance from each point in the diameter to the other not the path, and then using a double pointer to maintain the sequence of ≤s and The larger value of the maximum mxd comparing the answer and the distance between the end of the sequence and the point in the sequence. The maximum mxd value of a point in a sequence can be maintained with a monotone queue, how to find the diameter of the tree? First, the point of the maximum distance is calculated for any node dfs/bfs, and then the point is DFS/BFS to find the point with the maximum distance, and the path through the second traverse to the second point is the diameter of the tree.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineInc (I,J,K) for (int i=j;i<=k;i++)5 #defineMAXN 3000106 using namespacestd;7 8InlineintRead () {9 CharCh=getchar ();intf=1, x=0;Ten while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();} One while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); A returnf*x; - } - intN,S,D[MAXN],F[MAXN],P[MAXN],ED[MAXN],MXD[MAXN],TOT,FR,TA,Q1[MAXN],Q2[MAXN],L,R,NOW,ANS,SM[MAXN]; the BOOLVIS[MAXN]; - structe{intT,w,n;}; E es[maxn*3];intg[maxn],ess; - voidPeintFintTintW) { -Es[++ess]= (e) {t,w,g[f]}; g[f]=ess; Es[++ess]= (e) {f,w,g[t]}; g[t]=ess; + } - voidDFS1 (intXintFA) { + for(intI=G[X];I;I=ES[I].N)if(ES[I].T!=FA) d[es[i].t]=d[x]+ES[I].W,DFS1 (es[i].t,x); A } at voidDFS2 (intx) { - for(intI=G[X];I;I=ES[I].N)if(I!=f[x]) d[es[i].t]=d[x]+es[i].w,f[es[i].t]=i^1, DFS2 (es[i].t); - } - voidDFS3 (intx) { - for(intI=G[X];I;I=ES[I].N)if(!vis[es[i].t]) -d[es[i].t]=d[x]+es[i].w,mxd[0]=max (mxd[0],D[ES[I].T]), vis[es[i].t]=1, DFS3 (es[i].t); in } - intMain () { toN=read (); S=read (); ess=1; Inc (I,1, N-1){intA=read (), B=read (), c=read (); PE (A,B,C);} +d[1]=0; DFS1 (1,0);intmx1=1; Inc (I,1, N)if(D[i]>d[mx1]) mx1=i; d[mx1]=0; f[mx1]=0; DFS2 (MX1); - intMX2=MX1; Inc (I,1, N)if(D[I]>D[MX2]) mx2=i; the for(intI=MX2;I;I=ES[F[I]].T) tot++,p[tot]=i,ed[tot]=es[f[i]].w,vis[i]=1; *Inc (I,1, tot) d[p[i]]=0, mxd[0]=0, DFS3 (P[i]), mxd[i]=mxd[0],sm[i]=sm[i-1]+Ed[i]; $L=1; R=1; now=ed[1]; Fr=ta=1; q1[1]=mxd[1]; q2[1]=1; ans=0x3fffffff;Panax Notoginseng while(l<=r&&l<=tot-1){ - while(r<=tot-1&&now<=s) { theAns=min (Ans,max (Max (q1[fr],sm[l-1]), sm[tot-1]-sm[r-1])); r++; + while(Fr<=ta&&mxd[r]>q1[ta]) ta--; Q1[++TA]=MXD[R]; Q2[ta]=r; now+=Ed[r]; A } the if(q2[fr]==l) fr++; NOW-=ED[L]; l++; + if(l<=tot-1&&now<=s) ans=min (Ans,max (Max (q1[fr],sm[l-1]), sm[tot-1]-Sm[r])); - } $Inc (I,1, tot) ans=min (Ans,max (Max (mxd[i],sm[i-1]), sm[tot-1]-sm[i-1])); $printf"%d", ans);return 0; -}
20160715
BZOJ2282[SDOI2011] Fire