拓扑排序

来源:互联网 发布:发烧碟推荐知乎 编辑:程序博客网 时间:2024/06/05 04:47
var  map:array[1..100,1..100] of boolean;  into:array[1..100] of longint;  n:longint;procedure init;var  i,j:longint;begin  fillchar(map,sizeof(map),0);  fillchar(into,sizeof(into),0);  readln(n);  while not eof do    begin     readln(i,j);     map[i,j]:=true;     inc(into[j]);    end;end;procedure main;var  i,k,pos:longint;begin  for k:=1 to n do    begin     pos:=1;      while(pos<=n) and (into[pos]<>0) do inc(pos);     write(pos,' ');     into[pos]:=maxlongint;(只要置为非0就行了,不过这样无法判断该图是否可以进行拓扑排序)      fori:=1 to n do       if map[pos,i] then dec(into[i]);    end;end;begin  init;  main;end.


原创粉丝点击