Pagini recente » Istoria paginii runda/cel_mai_mare_olimpicar_2019_oni2009_zi1/clasament | Cod sursa (job #561356) | Cod sursa (job #914629) | Cod sursa (job #941009) | Cod sursa (job #128001)
Cod sursa(job #128001)
Utilizator |
|
Data |
25 ianuarie 2008 19:36:27 |
Problema |
Zvon |
Scor |
0 |
Compilator |
fpc |
Status |
done |
Runda |
Arhiva de probleme |
Marime |
2.03 kb |
type pelem=^elem;
elem=record
info:longint;
next:pelem;
end;
var fi,fo:text;
n,i,j,t,max,v1,v2,nm,ct,vl,valo,k:longint;
tati,nivel:array[1..100000]of pelem;
tmin,niv,fii:array[1..100000]of longint;
function part(st,dr:longint):longint;
var i,j,s,aux:longint;
begin
i:=st; j:=dr; s:=-1;
while i<j do
begin
if fii[i]<fii[j] then
begin
aux:=fii[i];
fii[i]:=fii[j];
fii[j]:=aux;
s:=-s;
end;
if s=1 then inc(i)
else dec(j);
end;
part:=i;
end;
procedure qsort(st,dr:longint);
var p:longint;
begin
if st<dr then
begin
p:=part(st,dr);
qsort(st,p-1);
qsort(p+1,dr);
end;
end;
procedure qin(var first:pelem; vl:longint);
var p:pelem;
begin
new(p);
p^.info:=vl;
p^.next:=first;
first:=p;
end;
procedure qout(var first:pelem; var vl:longint);
var p:pelem;
begin
vl:=first^.info;
p:=first;
first:=first^.next;
dispose(p);
end;
procedure solv;
begin
read(fi,n);
niv[1]:=1; nm:=0;
qin(nivel[1],1);
for i:=1 to n-1 do
begin
read(fi,v1,v2);
qin(tati[v1],v2);
niv[v2]:=niv[v1]+1;
qin(nivel[niv[v2]],v2);
if niv[v2]>nm then nm:=niv[v2];
end;
for i:=nm-1 downto 1 do
begin
while (nivel[i]<>nil) do
begin
qout(nivel[i],vl);
ct:=0;
while (tati[vl]<>nil) do
begin
qout(tati[vl],valo);
inc(ct);
fii[ct]:=valo;
end;
qsort(1,ct);
max:=-maxint;
for j:=1 to ct do
if tmin[fii[j]]+j>max then max:=tmin[fii[j]]+j;
tmin[vl]:=max;
end;
end;
writeln(fo,tmin[1]);
for i:=1 to n do
begin
niv[i]:=0;
tmin[i]:=0;
end;
if nivel[1]<>nil then qout(nivel[1],vl);
end;
begin
assign(fi,'zvon.in'); reset(fi);
assign(fo,'zvon.out'); rewrite(fo);
read(fi,t);
for k:=1 to t do
solv;
close(fi);
close(fo);
end.