STONE1 - Rải sỏi

Tác giả: flashmt

Ngôn ngữ: Pascal

const maxn=400;
var n:longint;
    s,f,b:array[1..maxn] of longint;
    a:array[1..maxn,1..maxn] of longint;

procedure rf;
var i,j:longint;
begin
     readln(n);
     while not eof do
     begin
          read(i,s[i]);
          for j:=1 to s[i] do read(a[i,j]);
          readln;
     end;
end;

procedure visit(x:longint);
var i,j,t:longint;
begin
     if s[x]=0 then
     begin
          f[x]:=1; exit;
     end;
     for i:=1 to s[x] do visit(a[x,i]);
     for i:=1 to s[x] do b[i]:=f[a[x,i]];
     for i:=1 to s[x]-1 do
         for j:=i+1 to s[x] do
             if b[i]<b[j] then
             begin
                  t:=b[i]; b[i]:=b[j]; b[j]:=t;
             end;
     f[x]:=b[1]; t:=b[1]-1;
     for i:=2 to s[x] do
         if t<=b[i] then
         begin
              f[x]:=f[x]+b[i]-t;
              t:=b[i]-1;
         end
         else dec(t);
end;

begin
     rf;
     visit(1);
     writeln(f[1]);
end.

Download