You can read this article and write it very clearly ...
http://z55250825.blog.163.com/blog/static/150230809201412793151890/
Then my monotonous queue was written out to be different from him ...
Max (F[i]+f[j]+j-i) J belongs to [I+1,I+SUM/2], for J, is not the Max (f[i]-i) +f[j]+j, maintenance f[i]-i increment is not OK?
So say the basic way of this ring-set tree ...
The first is to build the map
Then run Tarjan, run the time to record a certain point of the father, and then if a certain point x of a son exists Dfn[x]<low[too], then obviously X-son is a bridge ... This section can be seen as a node and a son in a tree dp. Then a point run after Tarjan and then find out all the fa[son]<>x and Dfn[x]<dfn[too], this is the x to too to x ring, this time ran a loop DP, just to note that X take too can not take, too take X can not take , two times for resolution ...
Tree as a tree, ring when the ring processing ...
typearr=RecordToward,next:longint; End; ConstMAXM=3000000; MAXN=300000; varEdge:Array[0.. MAXM] ofarr; FIRST,NUM,A,F,P,DFN,LOW,DEP,FA:Array[0.. MAXN] ofLongint; N,m,time,tot,ans:longint; functionMax (x,y:longint): Longint;begin ifX<y Thenexit (y); Exit (x);End; functionmin (x,y:longint): Longint;begin ifX<y Thenexit (x); Exit (y);End; procedureAddedge (i,j:longint);beginInc (TOT); Edge[tot].toward:=J; Edge[tot].next:=First[i]; First[i]:=tot;End; procedureDP (x,root:longint);varSum,head,tail,i:longint;beginsum:=dep[x]-dep[root]+1; I:=x; whileSum>0 Do beginNum[sum]:=F[x]; X:=Fa[x]; Dec (sum); End; X:=i; Sum:=dep[x]-dep[root]+1; fori:=1 toSum DoNum[i+sum]:=Num[i]; Head:=1; Tail:=1; p[1]:=1; fori:=2 tosum<<1 Do begin while(Head<=tail) and(p[head]+sum>>1<i) DoInc (head); Ans:=max (ans,num[p[head]]-p[head]+num[i]+i); while(Head<=tail) and(num[p[tail]]-p[tail]<num[i]-i) DoDec (tail); Inc (tail); P[tail]:=i; End; fori:=2 toSum DoF[root]:=max (F[root],min (i-1, sum-i+1)+num[i]);End; procedureTarjan (x:longint);varI,too:longint;beginInc (time); DFN[X]:=Time ; LOW[X]:=Time ; I:=First[x]; whileI>0 Do begintoo:=Edge[i].toward; ifTOO<>FA[X] Then begin ifdfn[too]=0 Then beginDep[too]:=dep[x]+1; Fa[too]:=x; Tarjan (too); ifLOW[TOO]<LOW[X] Thenlow[x]:=Low[too]; End Else ifDFN[TOO]<LOW[X] Thenlow[x]:=Dfn[too]; ifDfn[x]<low[too] Then beginans:=max (ans,f[x]+f[too]+1); F[X]:=max (f[x],f[too]+1); End; End; I:=Edge[i].next; End; I:=First[x]; whileI>0 Do begintoo:=Edge[i].toward; if(fa[too]<>x) and(Dfn[x]<dfn[too]) ThenDP (TOO,X); I:=Edge[i].next; End;End; procedureinto ;varI,j,k:longint;beginreadln (n,m); fori:=1 toM Do beginRead (k,a[1]); forj:=2 toK Do beginread (a[j]); Addedge (A[j],a[j-1]); Addedge (A[j-1],a[j]); End; End;End; procedureWork ;beginTime :=0; Ans:=0; dep[1]:=1; Tarjan (1); Writeln (ans);End; begininto ; work;End.
View Code
(Have time to engage in the insane dynamic Cactus)
bzoj1023: [Shoi2008]cactus Cactus Chart