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

DP solution, but why WA?
Posted by Vlad Ionescu 11 Jun 2003 21:21
Can anyone tell me what is wrong? I tested it a lot and everything
seems correct, I even ignore branches with 0 apples... It passed all
tests on this webboard,yet it's still WA! I used DP to try to solve
it, here is the code:

var a:array[1..100,1..100] of longint;
    m,g,min:array[0..100] of longint;
    v:array[1..100] of 0..1;
    n,i,j,k,l,q:longint;
procedure df(x,t:integer);
var i:integer;
begin
  v[x]:=1;
  g[x]:=0; m[x]:=0;
  for i:=1 to n do
      if (a[x,i]<>0) and (v[i]=0) then begin
         df(i,x); g[x]:=g[x]+g[i]; m[x]:=m[x]+m[i];
         end;
  if t<>0 then begin
     g[x]:=g[x]+1;
     m[x]:=m[x]+a[t,x];
     end;
end;
begin
  fillchar(a,sizeof(a),0);
  fillchar(v,sizeof(v),0);
  {assign(input,'input.txt'); reset(input);}
  readln(n,q);
  for k:=1 to n-1 do begin
      readln(i,j,l);
      a[i,j]:=l; a[j,i]:=l;
      if l=0 then dec(q);
      end;
  df(1,0);
  l:=m[1];
  for i:=1 to n do min[i]:=maxint;
  min[0]:=0;
  for i:=1 to n-1 do
      for j:=i+1 to n do
          if g[i]>g[j] then begin
             k:=g[i]; g[i]:=g[j]; g[j]:=k;
             k:=m[i]; m[i]:=m[j]; m[j]:=k;
             end;
  for i:=1 to n do
      for j:=n-g[i]-1 downto 0 do
          if min[j]+m[i]<min[j+g[i]] then min[j+g[i]]:=min[j]+m[i];
  writeln(l-min[q]);
end.

My e-mail is vlad_io1@yahoo.com, if you don't understand the code or
know what's wrong just mail me pls. Thanks!
I`ve same status.... but I know our WA is about branches with zero apples(-)
Posted by Locomotive 12 Jun 2003 20:12
Re: I`ve same status.... but I know our WA is about branches with zero apples(-)
Posted by vladu adrian 13 Jun 2003 01:21
> Impossible. If you made a good DP, you shouldn't havw any trouble
solving it correctly.