Pagini recente » Cod sursa (job #415718) | Cod sursa (job #1241630) | Cod sursa (job #275249) | Cod sursa (job #1691471) | Cod sursa (job #103879)
Cod sursa(job #103879)
type pnod=^nod;
nod=record
t:longint;
urm:pnod;
end;
var t,max,nrmax:array[0..100010]of longint;
v:array[0..100010]of pnod;
tt,n,i,k,a,b:longint;
aux:pnod;
procedure df(nod:longint);
var x:pnod;
begin
x:=v[nod]^.urm;
while x <> nil do
begin
df(x^.t);
if t[x^.t]=max[nod] then
inc(nrmax[nod]);
if t[x^.t]>max[nod] then
begin
max[nod]:=t[x^.t];
nrmax[nod]:=1;
end;
x:=x^.urm;
end;
if nod<>0 then
t[nod]:=max[nod]+nrmax[nod];
end;
begin
assign(input,'zvon.in');reset(input);
assign(output,'zvon.out');rewrite(output);
readln(tt);
for k:=1 to tt do
begin
readln(n);
fillchar(t,sizeof(t),0);
fillchar(nrmax,sizeof(nrmax),0);
fillchar(max,sizeof(max),0);
for i:=1 to n do
begin
new(v[i]);
v[i]^.urm:=nil;
end;
for i:=1 to n-1 do
begin
readln(a,b);
aux:=new(pnod);
aux^.t:=b;
aux^.urm:=v[a]^.urm;
v[a]^.urm:=aux;
end;
if n<>1 then
df(1);
writeln(t[1]);
//stergere lista
for i:=1 to n-1 do
begin
while (v[i]^.urm<>nil) do
begin
aux:=v[i]^.urm;
v[i]:=aux;
dispose(aux);
end;
end;
end;
close(input);close(output);
end.