Pagini recente » Cod sursa (job #27045) | Cod sursa (job #2023361) | Cod sursa (job #460799) | Cod sursa (job #221541) | Cod sursa (job #751791)
Cod sursa(job #751791)
Program p1;
var bufi,bufa:array[0..1 shl 20] of char;
rang,tata : array[1..100000] of longint;
n,m,x,y,i,oper1 : longint;
Function radacina(x:longint):longint;
var aux,q : longint;
begin
q:=x;
while tata[q]<>q do q:=tata[q];
radacina:=q;
while tata[x]<>x do begin aux:=tata[x]; tata[x]:=q; x:=aux; end;
end;
Procedure uneste(x,y:longint);
begin
if rang[x]>rang[y] then tata[y]:=x
else tata[x]:=y;
if rang[x]=rang[y] then rang[y]:=rang[y]+1;
end;
begin
assign(input,'disjoint.in'); reset(input);
assign(output,'disjoint.out'); rewrite(output);
settextbuf(input,bufi); settextbuf(output,bufa);
readln(n,m);
for i:=1 to n do begin
tata[i]:=i;
rang[i]:=1;
end;
for i:=1 to m do begin
readln(oper1,x,y);
if oper1=1 then uneste(radacina(x),radacina(y))
else begin
if radacina(x)=radacina(y) then writeln('DA')
else writeln('NU');
end;
end;
close(input); close(output);
end.