Pagini recente » Cod sursa (job #2124492) | Cod sursa (job #2359277) | Cod sursa (job #3139065) | Cod sursa (job #1960121) | Cod sursa (job #401533)
Cod sursa(job #401533)
const infile='disjoint.in';
outfile='disjoint.out';
maxnm=100001;
var tata,niv:array[0..maxnm]of longint;
n,m,x,y,i,j:longint;
fi,fo:text;
op:1..2;
procedure unire(x,y:longint);
begin
if(niv[x]>niv[y])then tata[y]:=x
else begin
tata[x]:=y;
if(niv[x]=niv[y])then inc(niv[y]);
end;
end;
function gaseste(i:longint):longint;
var y,t:longint;
begin
y:=i; while(tata[y]<>y)do y:=tata[y];
while(i<>tata[i])do begin t:=tata[i]; tata[i]:=y; i:=t; end;
gaseste:=y;
end;
begin
assign(fi,infile); reset(fi);
assign(fo,outfile); rewrite(fo);
readln(fi,n,m);
for x:=1 to n do begin tata[x]:=x; niv[x]:=1; end;
while(m>0)do begin
readln(fi,op,x,y); dec(m);
i:=gaseste(x); j:=gaseste(y);
if(op=2)then
if(i=j)then writeln(fo,'DA')
else writeln(fo,'Nu')
else if(i<>j)then unire(i,j);
end;
close(fi); close(fo);
end.