How can I not Memory Limit Exceeded?
Why do I memory limit exceeded even if I have use pointer?
Here is my code:
const mn=6000;
inf=-100000000;
type rec=record
father:integer;
num:integer;
child:array[1..mn]of ^integer;
end;
var value:array[1..mn]of ^integer;
f:array[1..mn,1..2]of ^longint;
t:array[1..mn]of ^rec;
n,i,a,b,root:integer;
ans:longint;
function max(a,b:longint):longint;
begin
if a>b
then exit(a)
else exit(b);
end;
function dp(root,lab:integer):longint;
var i:integer;
begin
if root=0 then exit(0);
if f[root,lab]^>inf then exit(f[root,lab]^);
if lab=1
then begin
f[root,lab]^:=value[root]^;
for i:=1 to t[root]^.num do
inc(f[root,1]^,dp(t[root]^.child[i]^,2));
end
else begin
f[root,lab]^:=0;
for i:=1 to t[root]^.num do
inc(f[root,2]^,max(dp(t[root]^.child[i]^,1),dp(t[root]^.child[i]^,2)));
end;
exit(f[root,lab]^);
end;
begin
readln(n);
for i:=1 to n do
begin
new(value[i]);
new(t[i]);
new(f[i,1]);
new(f[i,2]);
readln(value[i]^);
end;
while not eof do
begin
readln(a,b);
if(a=0)and(b=0)then break;
t[a]^.father:=b;
inc(t[b]^.num);
new(t[b]^.child[t[b]^.num]);
t[b]^.child[t[b]^.num]^:=a;
end;
for i:=1 to n do
if t[i]^.father=0
then begin
root:=i;
break;
end;
for i:=1 to n do
begin
f[i,1]^:=inf;
f[i,2]^:=inf;
end;
ans:=max(dp(root,1),dp(root,2));
writeln(ans);
end.
Who can HELP me?
Edited by author 05.04.2010 16:57