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

feel puzzled by 1018
Posted by lilith 9 Oct 2004 12:56
I got WA in the 11th data, and I've readen the discussion in the webboard, but them made me even more puzzled. What should I do with the branch which is 0 on earth? And what about when n=q? Would you please help me?
And this is my program, I will be very thankful if you can tell me what's wrong with my program too.
First, I use e and cost to record the nodes and numbers of apples. Then I build up a tree, delete the branch which is on the top and with the fewest apples. When k=n-1-m, the answer comes out.
As follow:
type aas=record
     bef:shortint;
     val:integer;
     nex:array[1..2]of byte;
end;
var i,j,k,t,n,m,a,b,c:integer;
    ans:longint;
    tree:array[1..100]of aas;
    e:array[1..100,1..3]of shortint;
    cost:array[1..100,1..3]of integer;
    num:array[1..100]of shortint;
    mark:array[1..100]of shortint;

procedure find(aa:integer);
var i2:integer;
begin
     for i2:=1 to num[aa] do if mark[e[aa,i2]]=0 then begin
         inc(mark[aa]);
         tree[aa].nex[mark[aa]]:=e[aa,i2];
         tree[e[aa,i2]].bef:=aa;
         tree[e[aa,i2]].val:=cost[aa,i2];
     end;
     for i2:=1 to mark[aa] do find(tree[aa].nex[i2]);
end;

begin
     readln(n,m);
     fillchar(e,sizeof(e),0);
     fillchar(num,sizeof(num),0);
     fillchar(cost,sizeof(cost),0);
     ans:=0;
     for i:=1 to n-1 do begin
         read(a,b);
         readln(c);
         if c>0 then begin
            inc(num[a]);
            inc(num[b]);
            e[a,num[a]]:=b;
            e[b,num[b]]:=a;
            cost[a,num[a]]:=c;
            cost[b,num[b]]:=c;
            ans:=ans+c;
         end;
     end;
     fillchar(tree,sizeof(tree),0);
     fillchar(mark,sizeof(mark),0);
     find(1);
     k:=0;
     while k<n-1-m do begin
           c:=-1;
           for i:=1 to n do if (mark[i]=0)and((c=-1)or(c>tree[i].val)) then begin
               c:=tree[i].val;
               t:=i;
           end;
           inc(k);
           mark[t]:=-1;
           ans:=ans-c;
           dec(mark[tree[t].bef]);
     end;
     writeln(ans);
end.