BZOJ 1023

时间:2024-01-07 08:16:26
program bzoj1023;
uses math; const inf=;
maxn=;
maxm=; type edge=record
togo,next:longint;
end; var n,m,cnt,ind,ans,l,r:longint;
last,deep,f,low,dfn,fa:array [..maxn] of longint;
a,q:array [..maxn*] of longint;
e:array [..maxm] of edge; procedure insert(u,v:longint);
begin
inc(cnt);
e[cnt].togo:=v; e[cnt].next:=last[u]; last[u]:=cnt;
inc(cnt);
e[cnt].togo:=u; e[cnt].next:=last[v]; last[v]:=cnt;
end; procedure dp(root,x:longint);
var tot,i:longint;
begin
tot:=deep[x]-deep[root]+;
i:=x;
while i<>root do
begin
a[tot]:=f[i];
dec(tot);
i:=fa[i];
end;
a[tot]:=f[root];
tot:=deep[x]-deep[root]+;
for i:= to tot do
a[i+tot]:=a[i];
q[]:=; l:=; r:=;
for i:= to (tot shl ) do
begin
while (l<=r) and (i-q[l]>(tot shr )) do
inc(l);
ans:=max(ans,a[i]+i+a[q[l]]-q[l]);
while (l<=r) and (a[q[r]]-q[r]<=a[i]-i) do
dec(r);
inc(r);
q[r]:=i;
end;
for i:= to tot do
f[tot]:=max(f[root],a[i]+min(i-,tot-i+));
end; procedure dfs(x:longint);
var i:longint;
begin
inc(ind);
low[x]:=ind;
dfn[x]:=ind;
i:=last[x];
while i<> do
begin
if e[i].togo<>fa[x] then
begin
if dfn[e[i].togo]= then
begin
fa[e[i].togo]:=x;
deep[e[i].togo]:=deep[x]+;
dfs(e[i].togo);
low[x]:=min(low[x],low[e[i].togo]);
end
else
low[x]:=min(low[x],dfn[e[i].togo]);
if dfn[x]<low[e[i].togo] then
begin
ans:=max(ans,f[x]+f[e[i].togo]+);
f[x]:=max(f[x],f[e[i].togo]+);
end;
end;
i:=e[i].next;
end;
i:=last[x];
while i<> do
begin
if (fa[e[i].togo]<>x) and (dfn[x]<dfn[e[i].togo]) then
dp(x,e[i].togo);
i:=e[i].next;
end;
end; procedure main;
var i,j,a,b,k:longint;
begin
read(n,m);
for i:= to m do
begin
read(k,a);
for j:= to k do
begin
read(b);
insert(a,b);
a:=b;
end;
end;
dfs();
writeln(ans);
end; begin
main;
end.