这种tarjan+dp的水题我竟然还WA了两次,要小心!
1 type link=^node; 2 node=record 3 po:longint; 4 next:link; 5 end; 6 7 var rd,be,st,v,a,dp,dfn,low:array[0..500010] of longint; 8 f,b,bar:array[0..500010] of boolean; 9 edge,way:array[0..500010] of link; 10 h,t,i,n,m,beg,bs,s,x,y:longint; 11 p:link; 12 13 procedure add(y:longint;var q:link); 14 var p:link; 15 begin 16 new(p); 17 p^.po:=y; 18 p^.next:=q; 19 q:=p; 20 end; 21 22 function max(a,b:longint):longint; 23 begin 24 if a>b then exit(a) else exit(b); 25 end; 26 27 function min(a,b:longint):longint; 28 begin 29 if a>b then exit(b) else exit(a); 30 end; 31 32 procedure tarjan(x:longint); 33 var y:longint; 34 p:link; 35 36 begin 37 inc(h); 38 inc(t); 39 st[t]:=x; 40 dfn[x]:=h; 41 low[x]:=h; 42 f[x]:=true; 43 p:=way[x]; 44 while p<>nil do 45 begin 46 y:=p^.po; 47 if dfn[y]=0 then 48 begin 49 tarjan(y); 50 low[x]:=min(low[x],low[y]); 51 end 52 else if f[y] then low[x]:=min(low[x],low[y]); 53 p:=p^.next; 54 end; 55 if dfn[x]=low[x] then 56 begin 57 inc(s); 58 while st[t+1]<>x do 59 begin 60 y:=st[t]; 61 f[y]:=false; 62 be[y]:=s; 63 v[s]:=v[s]+a[y]; 64 dec(t); 65 end; 66 end; 67 end; 68 69 begin 70 readln(n,m); 71 for i:=1 to m do 72 begin 73 readln(x,y); 74 add(y,way[x]); 75 end; 76 for i:=1 to n do 77 readln(a[i]); 78 79 readln(beg,bs); 80 for i:=1 to bs do 81 begin 82 read(x); 83 b[x]:=true; 84 end; 85 86 for i:=1 to n do 87 if dfn[i]=0 then 88 begin 89 h:=0; 90 t:=0; 91 tarjan(i); 92 end; 93 94 for i:=1 to n do 95 begin 96 p:=way[i]; 97 while p<>nil do 98 begin 99 y:=p^.po; 100 if be[i]<>be[y] then 101 begin 102 add(be[i],edge[be[y]]); 103 inc(rd[be[i]]); 104 end; 105 p:=p^.next; 106 end; 107 if b[i] then bar[be[i]]:=true; 108 end; 109 110 t:=0; 111 for i:=1 to s do 112 if rd[i]=0 then 113 begin 114 inc(t); 115 st[t]:=i; 116 end; 117 118 h:=1; 119 while h<=t do 120 begin 121 x:=st[h]; 122 dp[x]:=max(dp[x],v[x]); 123 p:=edge[x]; 124 while p<>nil do 125 begin 126 y:=p^.po; 127 if bar[x] then 128 begin 129 dp[y]:=max(dp[y],dp[x]+v[y]); 130 bar[y]:=true; 131 end; 132 dec(rd[y]); 133 if rd[y]=0 then 134 begin 135 inc(t); 136 st[t]:=y; 137 end; 138 p:=p^.next; 139 end; 140 inc(h); 141 end; 142 writeln(dp[be[beg]]); 143 end.