Pagini recente » Cod sursa (job #925958) | Cei mai harnici utilizatori info-arena | Cod sursa (job #2871443) | Cod sursa (job #2344418) | Cod sursa (job #298050)
Cod sursa(job #298050)
type lista=^element;
element=record
nr:longint;
urm:lista;
end;
var i,n,m,c,a,b:longint;
f,f2:text;
elem,apl:array[1..100000] of longint;
liste:array[1..100000] of record
first,last:lista;
end;
p:lista;
procedure Uneste(x,y:longint);
var mx,my:longint;
t:lista;
begin
mx:=apl[x];
my:=apl[y];
if elem[mx]<elem[my] then
begin
t:=liste[mx].first;
while t<>nil do
begin
apl[t^.nr]:=my;
t:=t^.urm;
end;
liste[my].last^.urm:=liste[mx].first;
liste[my].last:=liste[mx].last;
end
else
begin
t:=liste[my].first;
while t<>nil do
begin
apl[t^.nr]:=mx;
t:=t^.urm;
end;
liste[mx].last^.urm:=liste[my].first;
liste[mx].last:=liste[my].last;
end;
end;
procedure Verifica(x,y:longint);
begin
if apl[x]=apl[y] then writeln(f2,'DA')
else writeln(f2,'NU');
end;
begin
assign(f,'disjoint.in');
assign(f2,'disjoint.out');
reset(f);
rewrite(f2);
readln(f,n,m);
for i:=1 to n do
begin
apl[i]:=i;
elem[i]:=1;
New(p);
p^.nr:=i;
p^.urm:=nil;
liste[i].first:=p;
liste[i].last:=p;
end;
for i:=1 to m do
begin
readln(f,c,a,b);
if c=1 then Uneste(a,b)
else Verifica(a,b);
end;
close(f);
close(f2);
end.