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 1018. Binary Apple Tree

Crash (stack overflow)
Posted by dypjill 16 Sep 2006 06:34
Why my program is wrong,who can help me.
  This is my program:
   var
  a:array [0..100,0..100] of longint;
  s,b:array [0..100] of longint;
  used:array [1..100] of boolean;
  i,j,m,n,q,l,k,ans:longint;
 procedure cal(k:longint);
  var
   i,j:longint;
  begin
   used[k]:=true;
   for i:=1 to n do
    if (a[k,i]<>-1) and (not used[i]) then
     begin
      cal(i);
      b[k]:=b[k]+b[i]+1;
      s[k]:=s[k]+s[i]+a[k,i];
     end;
  end;
 function del(k,m:longint):longint;
  var
   i,j,min,k1,k2,mink,ss,t:longint;
  begin
   if m=0 then begin
    del:=0;
    exit;
   end;
   used[k]:=true;
   if m=b[k]
    then del:=s[k]
    else begin
     k1:=0; k2:=0;
     for i:=1 to n do
      if (a[k,i]<>-1) and (not used[i]) then
       if k1=0 then k1:=i
               else k2:=i;
     min:=maxlongint;
     if m>b[k1] then min:=s[k1]+a[k,k1]+del(k2,m-b[k1]-1);
     if m>b[k2] then begin
      ss:=s[k2]+a[k,k2]+del(k1,m-b[k2]-1);
      if min>ss then min:=ss;
     end;
     for i:=m-b[k2] to b[k1] do begin
      ss:=del(k1,i);
      ss:=del(k2,m-i)+ss;
      if ss<min then min:=ss;
     end;
     del:=min;
    end;
   used[k]:=false;
  end;
 begin
  readln(n,q);
  for i:=0 to 100 do
   for j:=0 to 100 do
    a[i,j]:=-1;
  for k:=1 to n-1 do begin
   readln(i,j,l);
   a[i,j]:=l;
   a[j,i]:=l;
  end;
  fillchar(used,sizeof(used),false);
  cal(1);
  s[0]:=0; b[0]:=0;
  fillchar(used,sizeof(used),false);
  ans:=s[1]-del(1,n-q-1);
  writeln(ans);
 end.