Pagini recente » Cod sursa (job #443870) | Cod sursa (job #1839998) | Cod sursa (job #973172) | Cod sursa (job #1031734) | Cod sursa (job #458842)
Cod sursa(job #458842)
program disjoint;
const infile='disjoint.in';
outfile='disjoint.out';
maxn=100002;
var h,apa:array[0..maxn] of longint;
n,m:longint;
procedure re(x,y:longint);
begin
if h[x]>h[y] then apa[y]:=x
else apa[x]:=y;
if h[x]=h[y] then inc(h[y]);
end;
function ossz(x:longint):longint;
var y,r,t:longint;
begin
r:=x;
while apa[r]<>r do
r:=apa[r];
ossz:=r;
y:=x;
while apa[y]<>y do
begin
t:=apa[y];
apa[y]:=r;
y:=t;
end;
end;
procedure megold;
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
apa[i]:=i;
h[i]:=1;
end;
for k:=1 to m do
begin
readln(op,x,y);
i:=ossz(x);
j:=ossz(y);
if op=1 then
if i<>j then re(i,j)
else
else if i<>j then writeln('NU')
else writeln('DA');
end;
close(input); close(output);
end;
begin
megold;
end.