做过4010这题其实就水了

把图反向之后直接拓扑排序做即可,我们可以用链表来优化

每个航班的最小起飞序号就相当于在反向图中不用这个点最迟到哪

  1 type node=record
  2        po,next:longint;
  3      end;
  4 
  5 var e:array[0..10010] of node;
  6     ans,p,h,suc,du,d,a:array[0..2010] of longint;
  7     i,x,y,n,m:longint;
  8 
  9 procedure add(x,y:longint);
 10   begin
 11     e[i].po:=y;
 12     e[i].next:=p[x];
 13     p[x]:=i;
 14   end;
 15 
 16 procedure merge(x,y:longint);
 17   begin
 18     if h[x]=0 then h[x]:=h[y]
 19     else begin
 20       x:=h[x];
 21       while suc[x]<>0 do x:=suc[x];
 22       suc[x]:=h[y];
 23     end;
 24   end;
 25 
 26 function min(wh:longint):longint;
 27   var i,x,y,t,b:longint;
 28   begin
 29     for i:=1 to n do
 30     begin
 31       du[i]:=d[i];
 32       h[i]:=0;
 33     end;
 34     for i:=1 to n do
 35       if (i<>wh) and (d[i]=0) then
 36       begin
 37         suc[i]:=h[a[i]];
 38         h[a[i]]:=i;
 39       end;
 40     t:=n;
 41     while t>0 do
 42     begin
 43       x:=h[t];
 44       if x=0 then exit(t);
 45       h[t]:=suc[h[t]];
 46       i:=p[x];
 47       while i<>0 do
 48       begin
 49         y:=e[i].po;
 50         dec(du[y]);
 51         if (y<>wh) and (du[y]=0) then
 52         begin
 53           if a[y]<t then b:=a[y]
 54           else b:=t;
 55           suc[y]:=h[b];
 56           h[b]:=y;
 57         end;
 58         i:=e[i].next;
 59       end;
 60       merge(t-1,t);
 61       dec(t);
 62     end;
 63     exit(0);
 64   end;
 65 
 66 procedure work1;
 67   var i,x,y,t,b:longint;
 68   begin
 69     for i:=1 to n do
 70     begin
 71       du[i]:=d[i];
 72       h[i]:=0;
 73     end;
 74     for i:=1 to n do
 75       if d[i]=0 then
 76       begin
 77         suc[i]:=h[a[i]];
 78         h[a[i]]:=i;
 79       end;
 80     t:=n;
 81     while t>0 do
 82     begin
 83       x:=h[t];
 84       ans[t]:=x;
 85       h[t]:=suc[h[t]];
 86       i:=p[x];
 87       while i<>0 do
 88       begin
 89         y:=e[i].po;
 90         dec(du[y]);
 91         if du[y]=0 then
 92         begin
 93           if a[y]<t then b:=a[y]
 94           else b:=t;
 95           suc[y]:=h[b];
 96           h[b]:=y;
 97         end;
 98         i:=e[i].next;
 99       end;
100       merge(t-1,t);
101       dec(t);
102     end;
103     for i:=1 to n-1 do
104       write(ans[i],' ');
105     writeln(ans[n]);
106   end;
107 
108 procedure work2;
109   var i:longint;
110   begin
111     for i:=1 to n-1 do
112       write(min(i),' ');
113     writeln(min(n));
114   end;
115 
116 begin
117   readln(n,m);
118   for i:=1 to n do
119   begin
120     read(a[i]);
121     if a[i]>n then a[i]:=n;
122   end;
123   for i:=1 to m do
124   begin
125     readln(x,y);
126     add(y,x);
127     inc(d[x]);
128   end;
129   work1;
130   work2;
131 end.
View Code

 

posted on 2015-06-30 10:19  acphile  阅读(180)  评论(0编辑  收藏  举报