- 拓扑编号
- 2014-10-27 10:07:45 @
type
ss=record
dt,nt:longint;
end;
var
hd,rd,p:array[1..100000]of longint;
heap:array[1..100000]of longint;
inheap:array[1..100000]of boolean;
edge:array[1..200000]of ss;
tmp,i,n,m,x,y,tot,q:longint;
procedure swap(var x,y:longint);var t:longint;begin t:=x; x:=y; y:=t; end;
procedure down(x:longint);
var p1,p2,M:longint;
begin
p1:=x<<1; p2:=p1+1;
while(p1<=q)do
begin
if(heap[p1]>heap[p2])or(p1=q)then M:=p1 else M:=p2;
if(heap[M]>heap[x])then
begin
swap(heap[M],heap[x]);
x:=M; p1:=x<<1; p2:=p1+1;
end else break;
end;
end;
procedure up(x:longint);
var fa:longint;
begin
fa:=x>>1;
while(fa<>0)and(heap[x]>heap[fa])do
begin
swap(heap[x],heap[fa]);
x:=fa; fa:=x>>1;
end;
end;
procedure add(x:longint);begin inc(q); heap[q]:=x; up(q); end;
procedure delete;begin heap[1]:=heap[q]; dec(q); down(1); end;
procedure add_edge(x,y:longint);
begin
inc(tot);
edge[tot].nt:=hd[x];
edge[tot].dt:=y;
hd[x]:=tot;
end;
begin
assign(input,'1790.in'); assign(output,'1790.out');
reset(input); rewrite(output);
read(n,m);
for i:=1 to m do
begin
read(x,y);
add_edge(y,x);
inc(rd[x]);
end;
tot:=n+1;
while(tot>1)do
begin
for i:=1 to n do if(rd[i]=0)and(p[i]=0)and(not inheap[i])then
begin
add(i);
inheap[i]:=true;
end;
tmp:=hd[heap[1]];
while(tmp<>0)do
begin
dec(rd[edge[tmp].dt]);
tmp:=edge[tmp].nt;
end;
dec(tot); p[heap[1]]:=tot;
inheap[i]:=false; delete;
end;
for i:=1 to n do write(p[i],' ');
close(output);
end.
问问哪里写搓了...最后的三个过不去....
1 条评论
-
doc LV 10 MOD @ 2014-10-28 03:56:10
你本身while(tot>1)这一层循环就要跑O(n)规模次.
结果你内部还嵌套了一层for i:=1 to n.
你的时间复杂度就一下子跑到O(n^2)了, 当然是超时的.
- 1