I and OI
Past...
type  enode=record
      v,n:longint;
end;
const maxn=10001;
      maxm=50001;
type  node=array[0..maxn] of longint;
var   st,ins,low,dfn,num,idx:node;
      didx,ccnt,top:longint;
      e:array[0..maxm] of enode;
      h:array[0..maxn] of longint;
      cnt,n,m:longint;

      procedure add(u,v:longint);
      begin
            inc(cnt);
            e[cnt].v:=v;
            e[cnt].n:=h[u];
            h[u]:=cnt;
      end;

      function min(a,b:longint):longint;
      begin if a>b then exit(b); exit(a); end;

      procedure dfs(u:longint);
      var   v,p:longint;
      begin
            inc(didx);
            low[u]:=didx;
            dfn[u]:=didx;
            inc(top);
            st[top]:=u;
            ins[u]:=1;
            p:=h[u];
            while p<>-1 do
            begin
                  v:=e[p].v;
                  if dfn[v]=-1 then
                  begin
                        dfs(v);
                        low[u]:=min(low[u],low[v]);
                  end
                  else if ins[v]=1 then
                         low[u]:=min(low[u],dfn[v]);
                  p:=e[p].n;
            end;
            if low[u]=dfn[u] then
            begin
                  inc(ccnt); v:=0;
                  while u<>v do
                  begin
                        v:=st[top];
                        dec(top);
                        ins[v]:=0;
                        idx[v]:=ccnt;
                        inc(num[ccnt]);
                  end;
            end;
      end;

      procedure tarjan;
      var   o:longint;
      begin
            fillchar(low,sizeof(low),255);
            fillchar(dfn,sizeof(dfn),255);
            fillchar(st,sizeof(st),255);
            fillchar(ins,sizeof(ins),0);
            fillchar(num,sizeof(num),0);
            fillchar(idx,sizeof(idx),0);
            ccnt:=0;
            for o:=1 to n do
            begin
                  top:=0; didx:=0;
                  if dfn[o]=-1 then dfs(o);
            end;
      end;

      procedure init;
      var   i,u,v:longint;
      begin
            fillchar(h,sizeof(h),255);
            readln(n,m);
            for i:=1 to m do
            begin
                  readln(u,v);
                  add(u,v);
            end;
      end;
begin
      Init;
      Tarjan;
end.

  

posted on 2011-11-09 18:53  exponent  阅读(274)  评论(0编辑  收藏  举报