Pagini recente » Cod sursa (job #2293983) | Cod sursa (job #51675) | Cod sursa (job #740135) | Cod sursa (job #3193909) | Cod sursa (job #269047)
Cod sursa(job #269047)
var f,g:text;
t:array[1..100000] of longint;
rang:array[1..100000] of longint;
procedure citire;
var n,m,i,x,y,op:longint;
function tata(x:longint):longint;
var xx,y:longint;
begin
xx:=x;
while t[x]<>x do x:=t[x];
while t[xx]<>xx do begin {x in loc de xx}
y:=t[xx];
t[xx]:=x;
xx:=y; end;
tata:=t[x];
end;
procedure merge(x,y:longint);
begin
if rang[x]>rang[y] then t[y]:=x else t[x]:=y;
if rang[x]=rang[y] then rang[y]:=rang[y]+1;{creste rang cu unu}
end;
begin
assign(f,'disjoint.in');
reset(f);
readln(f,n,m);
assign(g,'disjoint.out');
rewrite(g);
for i:=1 to n do t[i]:=i;
for i:=1 to m do begin
readln(f,op,x,y);
if op=2 then begin
if tata(x)=tata(y) then writeln(g,'DA') else writeln(g,'NU');
end else begin
merge(tata(x),tata(y));
end;
end;
close(g);
end;
begin
citire;
end.