Pagini recente » Cod sursa (job #723029) | Cod sursa (job #2811873) | Cod sursa (job #3148149) | Cod sursa (job #424290) | Cod sursa (job #946915)
Cod sursa(job #946915)
program paduri_de_multimi_disjuncte;
var a:array[1..100000] of longint;
n,m,i,x,y:longint;
bufin,bufout:array[1..100000]of byte;
op:byte;
function parent(x:longint):longint;
var y:longint;
begin
if x=a[x] then parent:=x
else begin
y:=parent(a[x]);
parent:=y;
a[x]:=y;
end;
end;
begin
assign(input,'disjoint.in');
reset(input);
settextbuf(input,bufin);
assign(output,'disjoint.out');
rewrite(output);
settextbuf(output,bufout);
readln(n,m);
for i:=1 to n do a[i]:=i;
for i:=1 to m do
begin
readln(op,x,y);
case op of
1: a[parent(x)]:=parent(y);
2: if parent(x)=parent(y) then writeln('DA') else writeln('NU');
end;
end;
close(input);close(output);
end.