P1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会
裸的强连通
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 begin
12 with b[i] do
13 begin
14 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 begin
22 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 do
29 begin
30 v:=b[e].t;
31 if bfsdgr[v]=0 then
32 begin
33 tarjan(v);
34 if low[v]<low[x] then low[x]:=low[v];
35 end
36 else if (p[v]) and (bfsdgr[v]<low[x]) then
37 low[x]:=bfsdgr[v];
38 e:=b[e].f;
39 end;
40 if bfsdgr[x]=low[x] then
41 begin
42 inc(ans);
43 //writeln(x);
44 //repeat
45 while f[num]<>x do
46 begin
47 p[f[num]]:=false;
48 dec(num);
49 end;
50 //until f[num]=x;
51 end;
52 end;
53 begin
54 readln(n,m);
55 for i:=1 to m do
56 begin
57 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 do
63 if bfsdgr[i]=0 then tarjan(i);
64 writeln(ans);
65 end.
(转载请注明出处:http://www.cnblogs.com/Kalenda/)