标签:io ar for sp on cti bs ad new
解决方案1: type point = ^node; node = record i:longint; n:point; end; var i,j,k,n,m,a,b,top:longint; map:array[1..10000]of point; ans:array[1..10000]of longint; stack,indegree:array[1..10000]of longint; black:array[1..10000]of boolean; tp:point; procedure push(x:longint); begin inc(top); stack[top]:=x; end; function pop:longint; begin if(top=0)then exit(0); pop:=stack[top]; dec(top); end; function topo:boolean; var i,now:longint; tp:point; begin for i:=1 to n do if(indegree[i]=0)then push(i); for i:=1 to n do begin repeat now:=pop(); until(now=0)or(not black[now]); if(now=0)then exit(false); ans[i]:=now; black[now]:=true; tp:=map[now]; while(tp<>nil)do begin if(not black[tp^.i])and(indegree[tp^.i]>0)then begin dec(indegree[tp^.i]); if(indegree[tp^.i]=0)then push(tp^.i); end; tp:=tp^.n; end; end; exit(true);end; begin readln(n,m); for i:=1 to m do begin readln(a,b); tp:=map[a]; new(map[a]); map[a]^.i:=b; map[a]^.n:=tp; inc(indegree[b]); end; if(topo())then begin for i:=1 to n do write(ans[i],‘ ‘); writeln; end else writeln(‘Occur loop.‘); end.
标签:io ar for sp on cti bs ad new
原文地址:http://my.oschina.net/u/582827/blog/337737