BZOJ 1023

program bzoj1023;
uses math;

const inf=1000000000;
    maxn=50005;
    maxm=20000005;
    
type edge=record
    togo,next:longint;
end;
    
var n,m,cnt,ind,ans,l,r:longint;
    last,deep,f,low,dfn,fa:array [0..maxn] of longint;
    a,q:array [0..maxn*2] of longint;
    e:array [0..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]+1;
    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]+1;
    for i:=1 to tot do 
        a[i+tot]:=a[i];
    q[1]:=1; l:=1; r:=1;
    for i:=2 to (tot shl 1) do
        begin
            while (l<=r) and (i-q[l]>(tot shr 1)) 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:=2 to tot do 
    f[tot]:=max(f[root],a[i]+min(i-1,tot-i+1));
end;

procedure dfs(x:longint);
var i:longint;
begin
    inc(ind);
    low[x]:=ind;
    dfn[x]:=ind;
    i:=last[x];
    while i<>0 do
        begin
          if e[i].togo<>fa[x] then 
            begin
                if dfn[e[i].togo]=0 then 
                    begin
                        fa[e[i].togo]:=x;
                        deep[e[i].togo]:=deep[x]+1;
                        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]+1);
                      f[x]:=max(f[x],f[e[i].togo]+1);
                    end;
            end;
          i:=e[i].next;
        end;
    i:=last[x];
    while i<>0 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:=1 to m do 
        begin
            read(k,a);
            for j:=2 to k do 
            begin
                read(b);
                insert(a,b);
                a:=b;
            end;
        end;
    dfs(1);
    writeln(ans);
end;

begin
      main;
end.

 

posted @ 2014-10-27 17:04  Kiss our dream  阅读(172)  评论(0编辑  收藏  举报