ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1039. Anniversary Party

How can I not Memory Limit Exceeded?
Posted by tsinZ_915 5 Apr 2010 16:56
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
Re: How can I not Memory Limit Exceeded?
Posted by hehe 29 Mar 2013 11:42
because u r using much memory.. run it on ideone.. u will come to know
Re: How can I not Memory Limit Exceeded?
Posted by Khujamurod Murtozakulov (Tashkent U of IT) 10 Dec 2018 17:39
This problem is too simple than you think :)

Edited by author 10.12.2018 17:39