| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
图可能不连通!!!数据:
4 4
1 2 2 1 3 4 4 3
ans:1 2 3 4
........................................................................
type
arr=record
point,next:longint;
end;
var
edge:array[0..500000] of arr;
head,stack,ans,low,dfn:array[0..5100] of longint;
m,n,i,j,k,l,s,x,y,t:longint;
function min(a,b:longint):longint;
begin
if a>b then exit(b)
else exit(a);
end;
procedure tarjan(dep:longint);
var
p,po,x:longint;
f:boolean;
begin
inc(t);
dfn[dep]:=t;
low[dep]:=t;
inc(stack[0]);
stack[stack[0]]:=dep;
p:=head[dep];
while p<>0 do
begin
po:=edge[p].point;
if low[po]=-1 then
begin p:=edge[p].next; continue; end;
if low[po]=0 then tarjan(po);
low[dep]:=min(low[dep],low[po]);
p:=edge[p].next;
end;
if dfn[dep]=low[dep] then
begin
for p:=stack[0] downto 1 do
begin
low[stack[p]]:=-2;
if stack[p]=dep then break;
end;
f:=true;
for po:=stack[0] downto p do
begin
x:=head[stack[po]];
while x<>0 do
begin
if low[edge[x].point]<>-2 then
begin f:=false; break; end;
x:=edge[x].next;
end;
if not f then break;
end;
if f then
for po:=stack[0] downto p do
begin
inc(ans[0]);
ans[ans[0]]:=stack[po];
end;
for po:=stack[0] downto p do
low[stack[po]]:=-1;
stack[0]:=p-1;
end;
end;
procedure sort(l,r: longint);
var i,j,x,y: longint;
begin
i:=l;j:=r;x:=ans[(l+r) div 2];
repeat
while ans[i]<x do inc(i);
while x<ans[j] do dec(j);
if not(i>j)
then begin
y:=ans[i];ans[i]:=ans[j];ans[j]:=y;
inc(i);dec(j);
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
begin
read(n);
while n<>0 do
begin
readln(m);
for i:=1 to n do
begin
head[i]:=0;
low[i]:=0;
end;
s:=0;
for i:=1 to m do
begin
read(x,y);
inc(s);
edge[s].point:=y;
edge[s].next:=head[x];
head[x]:=s;
end;
t:=0;
ans[0]:=0;
stack[0]:=0;
for i:=1 to n do
if low[i]=0 then tarjan(i);
if ans[0]=0 then writeln
else
begin
sort(1,ans[0]);
write(ans[1]);
for i:=2 to ans[0] do
write(' ',ans[i]);
writeln;
end;
read(n);
end;
end.
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator