Pagini recente » Profil Alexoticos | Diferente pentru utilizator/andreistanescu intre reviziile 25 si 24 | Atasamentele paginii Clasament ada9 | Monitorul de evaluare | Cod sursa (job #103655)
Cod sursa(job #103655)
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;
begin
i:=1;
m:=0;nr:=0;
v[nod]:=v[nod]^.urm;
while v[nod]<> nil do
begin
df(v[nod]^.t);
/// writeln(v[nod]^.t);
if t[v[nod]^.t]=max[nod] then
inc(nrmax[nod]);
//inc(nr);
if t[v[nod]^.t]>max[nod] then
begin
max[nod]:=t[v[nod]^.t];
nrmax[nod]:=1;
{ m:=t[v[nod]^.t];
nr:=1;}
end;
v[nod]:=v[nod]^.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]);
while v[1]<>nil do
writeln(v[1]^.t);
for i:=1 to n do
v[i]:=nil;
end;
close(input);close(output);
end.