Pagini recente » Cod sursa (job #3001951) | Cod sursa (job #290311) | Cod sursa (job #2929782) | Cod sursa (job #226423) | Cod sursa (job #680365)
Cod sursa(job #680365)
var v:array [0..20, 0..100000] of longint;
w1:array[1..100000] of longint;
r:array [1..100000] of longint;
i, j, n, m, x, y, z, t:longint;
f, g:text;
ok:boolean;
c:char;
buf1, buf2:array [1.. 1 shl 17] of char;
function cauta (fp:longint):longint;
var fy, fx, fj:longint;
begin
if w1[fp]=0 then cauta:=0 else
begin
if r[fp]<>0 then cauta:=r[fp] else
begin
fx:=fp; fy:=w1[fp];
fj:=1;
while (fx<>0) and (fy<>0) do
begin
while fy mod 2 = 0 do
begin
fy := fy div 2;
inc (fj);
end;
fx:=v[fj, fx];
fy:=fy div 2; inc (fj);
end;
r[fp]:=1+cauta(fx);
cauta:=r[fp];
end;
end;
end;
begin
assign (f, 'cerere.in'); settextbuf (f, buf1); reset (f);
assign (g, 'cerere.out'); settextbuf (g, buf2); rewrite (g);
readln (f, n);
for i := 1 to n do read (f, w1[i]);
for i := 1 to n-1 do
begin
read (f, x, y);
v[1, y]:=x;
end;
// initializarea dinamicii
ok:=true; i:=1;
while ok do
begin
ok:=false;
for j := 1 to n do
begin
if v[i, j]<>0 then
begin
v[i+1, j]:=v[i, v[i, j]];
if v[i+1, j] <> 0 then ok:=true;
end;
end;
inc (i);
end;
for i := 1 to n do if r[i]=0 then r[i]:= cauta(i);
for i := 1 to n do write (g, r[i], ' ');
close (f); close (g);
end.