Help!! Why I got 'Crash stack overlow' in #11???
Послано
Storm 16 дек 2008 20:36
My code:
type graph=^node;
node=record v:longint;next:graph;end;
var g:array[1..6000] of graph;
f:array[1..6000,0..1] of longint;
a,deg:array[1..6000] of longint;
n:longint;
function max(a,b:longint):longint;
begin
if a<b then exit(b) else exit(a);
end;
procedure init;
var i,x,y:longint;
p:graph;
begin
read(n);
for i:=1 to n do read(a[i]);
repeat
read(x,y);
if (x<>0) or (y<>0) then
begin
new(p);inc(deg[x]);
p^.v:=x;p^.next:=g[y];g[y]:=p;
end;
until (x=0) and (y=0);
fillchar(f,sizeof(f),0);
end;
function search(v,x:longint):longint;
var p:graph;
i:longint;
begin
if f[v,x]<>0 then exit(f[v,x]);
if x=1 then f[v,x]:=a[v];
p:=g[v];
if x=0 then
begin
while p<>nil do
begin
f[v,x]:=f[v,x]+max(search(p^.v,0),search(p^.v,1));
p:=p^.next;
end;
end
else begin
while p<>nil do
begin
f[v,x]:=f[v,x]+search(p^.v,0);
p:=p^.next;
end;
end;
exit(f[v,x]);
end;
procedure work;
var i,j,c,ans,k:longint;
begin
ans:=0;
for i:=1 to n do
if deg[i]=0 then
inc(ans,max(search(i,0),search(i,1)));
writeln(ans);
end;
begin
init;
work;
end.