標籤:
裸的強連通
1 const maxe=50001; 2 type 3 node=record 4 f,t:longint; 5 end; 6 var n,m,dgr,i,u,v,num,ans:longint; 7 bfsdgr,low,head,f:array[0..maxe] of longint; 8 b:array[0..maxe] of node; 9 p:array[0..maxe] of boolean;10 procedure insert(u,v:longint);11 begin12 with b[i] do13 begin14 f:=head[u];15 t:=v;16 end;17 head[u]:=i;18 end;19 procedure tarjan(x:longint);20 var e,v:longint;21 begin22 inc(dgr);23 e:=head[x]; if e=0 then exit;24 inc(num);25 bfsdgr[x]:=dgr; low[x]:=dgr;26 f[num]:=x; p[x]:=true;27 //28 while e<>0 do29 begin30 v:=b[e].t;31 if bfsdgr[v]=0 then32 begin33 tarjan(v);34 if low[v]<low[x] then low[x]:=low[v];35 end36 else if (p[v]) and (bfsdgr[v]<low[x]) then37 low[x]:=bfsdgr[v];38 e:=b[e].f;39 end;40 if bfsdgr[x]=low[x] then41 begin42 inc(ans);43 //writeln(x);44 //repeat45 while f[num]<>x do46 begin47 p[f[num]]:=false;48 dec(num);49 end;50 //until f[num]=x;51 end;52 end;53 begin54 readln(n,m);55 for i:=1 to m do56 begin57 readln(u,v);58 insert(u,v);59 end;60 fillchar(bfsdgr,sizeof(bfsdgr),0);61 fillchar(p,sizeof(p),false);62 for i:=1 to n do63 if bfsdgr[i]=0 then tarjan(i);64 writeln(ans);65 end.
View Code
(轉載請註明出處:http://www.cnblogs.com/Kalenda/)
P1654: [Usaco2006 Jan]The Cow Prom 奶牛舞會