Pagini recente » Cod sursa (job #334927) | Cod sursa (job #1681072) | Cod sursa (job #293883) | Cod sursa (job #1710591) | Cod sursa (job #103869)
Cod sursa(job #103869)
type pnod=^nod;
nod=record
t:longint;
urm:pnod;
end;
var x,t,p,max,nrmax:array[1..100010]of longint;
v:array[1..100010]of pnod;
tt,n,i,j,k,a,b:longint;
aux:pnod;
procedure df(nod:longint);
var i,m,nr:longint;
x:pnod;
begin
i:=1;
m:=0;nr:=0;
x:=v[nod]^.urm;
while x<> nil do
begin
df(x^.t);
/// writeln(v[nod]^.t);
if t[x^.t]=max[nod] then
inc(nrmax[nod]);
//inc(nr);
if t[x^.t]>max[nod] then
begin
max[nod]:=t[x^.t];
nrmax[nod]:=1;
{ m:=t[v[nod]^.t];
nr:=1;}
end;
x:=x^.urm;
end;
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);
inc(x[a]);
aux:=new(pnod);
aux^.t:=b;
aux^.urm:=v[a]^.urm;
v[a]^.urm:=aux;
{ v[a]:=new(pnod);
v[a]^.t:=b;
v[a]^.urm:=nil;}
inc(p[a]);
end;
if n<>1 then
df(1);
writeln(t[1]);
//stergere lista
aux:=new(pnod);
for i:=1 to n-1 do
begin
if x[i]>0 then
while (v[i]^.urm<>nil) do
begin
aux:=v[i]^.urm;
v[i]:=aux;
dispose(aux);
end;
end;
end;
close(input);close(output);
end.