Pagini recente » Cod sursa (job #2418192) | Cod sursa (job #172286) | Cod sursa (job #1598155) | Cod sursa (job #711307) | Cod sursa (job #371512)
Cod sursa(job #371512)
var tata,rang:array[1..100000]of longint;
n,m,i,x,y:longint;
f:byte;
function find(t:longint):longint;
var r,a:longint;
begin
r:=t;
while r<>tata[r] do r:=tata[r];
while tata[t]<>t do begin
a:=tata[t];
tata[t]:=r;
t:=a;
end;
find:=r;
end;
procedure uneste(a,b:longint);
begin
if rang[a]>rang[b] then tata[b]:=a
else tata[a]:=b;
if rang[a]=rang[b] then inc(rang[b]);
end;
begin
assign(input,'disjoint.in');reset(input);
assign(output,'disjoint.out');rewrite(output);
read(n,m);
for i:=1 to n do begin
tata[i]:=i;
rang[i]:=1;
end;
for i:=1 to m do begin
read(f,x,y);
if f=2 then begin
if find(x)=find(y) then writeln('DA')
else writeln('NU');
end
else uneste(find(x),find(y));
end;
close(output);
end.