Cod sursa(job #104256)

Utilizator ProtomanAndrei Purice Protoman Data 16 noiembrie 2007 00:08:39
Problema Zvon Scor 0
Compilator fpc Status done
Runda Happy Coding 2007 Marime 1.91 kb
var f1,f2:text;
    i,j,r,t,n,pt,sn:longint;
    v:array[1..1000] of longint;
    a:array[1..1000,0..1000] of longint;

procedure afla(x:longint);
var i,j,h,r,max,aux:longint;
    s:array[0..1000] of longint;
begin
        if a[x,a[x,0]]=1 then
                v[x]:=0
        else
        begin
                max:=0;
                h:=a[x,0];
                s[0]:=0;
                for i:=1 to h do
                begin
                        if v[a[x,i]]=-1 then
                                afla(a[x,i]);
                        s[i]:=v[a[x,i]]+1;
                end;
                for i:=1 to h-1 do
                        for j:=i+1 to h do
                                if s[i]>s[j] then
                                begin
                                        aux:=s[i];
                                        s[i]:=s[j];
                                        s[j]:=aux;
                                end;
                r:=0;
                for i:=h downto 0 do
                begin
                        if s[i]+r>max then
                                max:=s[i]+r;
                        inc(r);
                end;
                v[x]:=max;
        end;
end;


begin
        assign(f1,'zvon.in');
        reset(f1);
        assign(f2,'zvon.out');
        rewrite(f2);
        read(f1,t);
        for i:=1 to t do
        begin
                read(f1,n);
                for j:=1 to n do
                begin
                        a[j,0]:=0;
                        v[j]:=-1;
                end;
                v[n]:=-1;
                for j:=1 to n-1 do
                begin
                        read(f1,pt,sn);
                        inc(a[pt,0]);
                        a[pt,a[pt,0]]:=sn;
                end;
                afla(1);
                writeln(f2,v[1]);
        end;
        close(f1);
        close(f2);
end.