This is really not a problem. In short, it's a tree-shaped dp+ ring DP
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <queue>7#include <algorithm>8#include <vector>9 #defineM 50008Ten #defineEPS 1e-10 One #defineMO 10000 A #definell Long Long - using namespacestd; - ll Read () the { - CharCh=GetChar (); -ll x=0, f=1; - for(;ch<'0'|| Ch>'9'; ch=GetChar ()) + if(ch=='-') -f=-1; + for(; ch>='0'&&ch<='9'; ch=GetChar ()) Ax=x*Ten+ch-'0'; at returnx*F; - } - intfa[m],m,n,dfn[m],low[m],t,head[m],next[ $*m],u[ $*m],cnt,ans,f[m],h[m],a[2*m],q[2*M]; - voidJiaintA1,intA2) - { -cnt++; innext[cnt]=HEAD[A1]; -head[a1]=CNT; tou[cnt]=A2; + } - voiddpintXinty) the { * intlen=h[y]-h[x]+1; $ for(intI=y;i!=x;i=Fa[i])Panax Notoginsenga[len--]=F[i]; -a[len]=F[x]; thelen=h[y]-h[x]+1; + for(inti=len+1; i<=2*len;i++) Aa[i]=a[i-Len]; the intH=1, t=1; +q[1]=1; - for(intI=2; i<=2*len;i++) $ { $ for(; h<=t&&i-q[h]>len/2; h++); -Ans=max (i-q[h]+a[i]+A[q[h]],ans); - for(; h<=t&&a[q[t]]-q[t]<=a[i]-i;t--); theq[++t]=i; - }Wuyi for(intI=2; i<=len;i++) theF[x]=max (F[x],a[i]+min (i-1, len-i+1)); - } Wu voidTarjin (intx) - { Aboutdfn[x]=low[x]=++T; $ for(intI=head[x];i;i=Next[i]) - if(u[i]!=Fa[x]) - { - if(!Dfn[u[i]]) A { +fa[u[i]]=x; theh[u[i]]=h[x]+1; - Tarjin (U[i]); $low[x]=min (Low[x],low[u[i]]); the } the Else thelow[x]=min (Low[x],dfn[u[i]]); the if(low[u[i]]>Dfn[x]) - { inAns=max (ans,f[x]+f[u[i]]+1); theF[x]=max (f[x],f[u[i]]+1); the } About } the for(intI=head[x];i;i=Next[i]) the if(fa[u[i]]!=x&&dfn[u[i]]>Dfn[x]) the DP (X,u[i]); + } - intMain () the {Bayin=read (); them=read (); the for(intI=1; i<=m;i++) - { - intK=read (), a1=read (); the for(intj=2; j<=k;j++) the { the intA2=read (); the Jia (A1,A2); - Jia (a2,a1); thea1=A2; the } the }94Tarjin (1); theprintf"%d\n", ans); the return 0; the }98
Bzoj 1023: [Shoi2008]cactus cactus Diagram