Pagini recente » Cod sursa (job #2484367) | Cod sursa (job #155352) | Cod sursa (job #2762129) | Clasament teme_acmunibuc_2014_1_2 | Cod sursa (job #481888)
Cod sursa(job #481888)
{$s-}
var tata,rang:array[1..100001]of longint;
buf:array[1..1 shl 17]of char;
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);
settextbuf(input,buf);
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.