Pagini recente » Cod sursa (job #92988) | Cod sursa (job #873109) | Cod sursa (job #40281) | Cod sursa (job #2334687) | Cod sursa (job #401967)
Cod sursa(job #401967)
const infile='disjoint.in';
outfile='disjoint.out';
maxn=100002;
var h,tata:array[0..maxn] of longint;
n,m:longint;
procedure reuniune(x,y:longint);
begin
if h[x]>h[y] then tata[y]:=x
else tata[x]:=y;
if h[x]=h[y] then inc(h[y]);
end;
function multime(x:longint):longint;
var y,r,t:longint;
begin
r:=x; while tata[r]<>r do r:=tata[r];
multime:=r; y:=x;
while tata[y]<>y do begin t:=tata[y]; tata[y]:=r; y:=t;end;
end;
procedure solve;
var op:byte;
x,y,k,i,j:longint;
begin
assign(input,infile); reset(input);
assign(output,outfile); rewrite(output);
readln(n,m);
for i:=1 to n do begin tata[i]:=i; h[i]:=1; end;
for k:=1 to m do begin
readln(op,x,y);
i:=multime(x); j:=multime(y);
if op=1 then
if i<>j then reuniune(i,j)
else
else if i<>j then writeln('NU')
else writeln('DA');
end;
close(input); close(output);
end;
begin
solve;
end.