Pagini recente » Cod sursa (job #2094082) | Cod sursa (job #2309877) | Cod sursa (job #913085) | Cod sursa (job #3228351) | Cod sursa (job #26341)
Cod sursa(job #26341)
program arbore;
const
fin='arbore.in';
fout='arbore.out';
nmax=100000;
secmax=1000;
type
edge=^elem;
elem=record
x:longint;urm:edge;
end;
var
e:array[1..nmax] of edge;
v:array[1..nmax] of longint;
q:edge;
viz:array[1..nmax] of boolean;
lim:array[1..nmax] of longint;
dim:longint;
a:array[1..nmax] of longint;
c:array[1..secmax] of longint;
p:array[0..secmax,0..100000 shr 3+1] of byte;
st:array[1..nmax] of longint;
ist:longint;
poz:array[1..nmax] of longint;
s,tata:longint;
niv:longint;
spart:boolean;
ok:boolean;
start,stop:longint;
id:array[1..nmax] of longint;
i,j,n,m,x,y,sec:longint;
procedure insert(x,y:longint);
var
q:edge;
begin
new(q);
q^.urm:=e[x]^.urm;
e[x]^.urm:=q;
q^.x:=y;
end;
function find_sec(x:longint):longint;
begin
find_sec:=(x-1) div sec+1;
end;
function first(x:longint):boolean;
begin
first:=((x-1) mod sec)=0;
end;
function last(x:longint):boolean;
begin
last:=(x=n) or (x mod sec=0);
end;
procedure update(gr,s,k:longint);
begin
if k=0 then
begin
if p[gr,s shr 3] and (1 shl (s and 7))<>0 then
p[gr,s shr 3]:=p[gr,s shr 3] xor (1 shl (s and 7))
end
else
p[gr,s shr 3]:=p[gr,s shr 3] or (1 shl (s and 7));
end;
function next(x:longint):longint;
var
p:edge;
begin
p:=e[x];
if p^.urm=nil then
next:=0
else
begin
p:=p^.urm;
while (p^.urm<>nil)and(viz[p^.x]=true) do
p:=p^.urm;
if viz[p^.x]=false then
begin
next:=p^.x;
viz[p^.x]:=true;
e[x]:=p;
end
else
begin
e[x]:=p;
next:=0;
end;
end;
end;
begin
assign(input,fin);
assign(output,fout);
rewrite(output);
reset(input);
readln(n,m);
for i:=1 to n do
begin
new(e[i]);
e[i]^.urm:=nil;
end;
for i:=1 to n-1 do
begin
readln(x,y);
insert(x,y);
insert(y,x);
end;
ist:=1;
st[ist]:=1;
viz[1]:=true;
dim:=1;v[1]:=1;niv:=1;id[1]:=1;
poz[1]:=1;
while ist>0 do
begin
x:=next(st[ist]);
if x<>0 then
begin
inc(ist);
inc(niv);
st[ist]:=x;
inc(dim);v[dim]:=x;
poz[x]:=dim;
id[dim]:=niv;
end
else
begin
dec(ist);
dec(niv);
end;
end;
sec:=trunc(sqrt(n));
spart:=trunc(sqrt(n))<>sqrt(n);
for i:=1 to m do
begin
read(x);
if x=1 then {update}
begin
readln(tata,s);
x:=poz[tata];
start:=x;
if not(first(x)) then
begin
y:=find_sec(x);
j:=(y-1)*sec+1;
start:=j;
stop:=y*sec;
if stop>n then
stop:=n;
for j:=start to stop do
update(y,c[y]+a[j],0);
j:=start;
inc(c[y],s);
while j<x do
begin
dec(a[j],s);
inc(j);
end;
for j:=start to stop do
update(y,c[y]+a[j],1);
start:=stop+1;
end;
ok:=true;
while (start<n)and(ok) do
begin
stop:=start+sec-1;
if stop>n then
stop:=n;
if id[stop]<=id[x] then
ok:=false
else
begin
y:=find_sec(start);
for j:=start to stop do
update(y,c[y]+a[j],0);
inc(c[y],s);
for j:=start to stop do
update(y,c[y]+a[j],1);
start:=stop+1;
end;
end;
if not(ok) then
begin
y:=find_sec(start);
inc(c[y],s);
j:=start;
while (j<=stop) and(id[j]>id[x]) do
inc(j);
for dim:=start to stop do
update(y,c[y]+a[dim],0);
for dim:=j to stop do
dec(a[j],s);
for dim:=start to stop do
update(y,c[y]+a[j],1);
end;
end
else {query}
begin
readln(s);
dim:=sec;
if spart then
inc(dim);
ok:=false;
for j:=1 to dim do
if p[j,s shr 3] and (1 shl (s and 7))<>0 then
begin
ok:=true;
start:=(j-1)*sec+1;
stop:=j*sec;
if stop>n then
stop:=n;
for x:=start to stop do
if c[j]+a[x]=s then
begin
writeln(v[x]);
break;
end;
break;
end;
if not(ok) then
writeln(-1);
end;
end;
close(output);
close(input);
end.