P1654: [Usaco2006 Jan]The Cow Prom 奶牛舞会

时间:2022-08-04 18:29:05

裸的强连通

 const maxe=;
type
node=record
f,t:longint;
end;
var n,m,dgr,i,u,v,num,ans:longint;
bfsdgr,low,head,f:array[..maxe] of longint;
b:array[..maxe] of node;
p:array[..maxe] of boolean;
procedure insert(u,v:longint);
begin
with b[i] do
begin
f:=head[u];
t:=v;
end;
head[u]:=i;
end;
procedure tarjan(x:longint);
var e,v:longint;
begin
inc(dgr);
e:=head[x]; if e= then exit;
inc(num);
bfsdgr[x]:=dgr; low[x]:=dgr;
f[num]:=x; p[x]:=true;
//
while e<> do
begin
v:=b[e].t;
if bfsdgr[v]= then
begin
tarjan(v);
if low[v]<low[x] then low[x]:=low[v];
end
else if (p[v]) and (bfsdgr[v]<low[x]) then
low[x]:=bfsdgr[v];
e:=b[e].f;
end;
if bfsdgr[x]=low[x] then
begin
inc(ans);
//writeln(x);
//repeat
while f[num]<>x do
begin
p[f[num]]:=false;
dec(num);
end;
//until f[num]=x;
end;
end;
begin
readln(n,m);
for i:= to m do
begin
readln(u,v);
insert(u,v);
end;
fillchar(bfsdgr,sizeof(bfsdgr),);
fillchar(p,sizeof(p),false);
for i:= to n do
if bfsdgr[i]= then tarjan(i);
writeln(ans);
end.

(转载请注明出处:http://www.cnblogs.com/Kalenda/)