如果发现广告等破坏行为,请尽量将条目恢复到较早的版本而不是把相应内容直接删除,谢谢合作。

拓扑排序

来自"NOCOW"

跳转到: 导航, 搜索

拓扑排序 找入度为0的点,删去与其相连的所有边,不断重复这一过程。


var a:array[1..100,1..100]of longint;
    v:array[1..100]of longint;
    b:array[1..100]of boolean;
    i,j,k,l,n:longint;
begin
  fillchar(b,sizeof(b),true);
  readln(n);
  for i:=1 to n do
    for j:=1 to n do
      begin
        read(a[i,j]);
        if a[i,j]=1 then
          inc(v[j]);
      end;
  for i:=1 to n do
      for j:=1 to n do
        if(b[j])and(v[j]=0) then
          begin
            write(j,' ');
            for  k:=1 to n do
              if a[j,k]=1 then dec(v[k]);
            b[j]:=false;
            break;
          end;
end.

拓扑排序不唯一,如果有环则无法完成拓扑排序

by 357795229
个人工具