Cod sursa(job #1420422)

Utilizator ButnaruButnaru George Butnaru Data 18 aprilie 2015 14:21:41
Problema Paduri de multimi disjuncte Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.71 kb
program disjoint;
type vector1=array[0..100001] of longint;
var n,m,i,j,tip,x,y:longint; sz,tata:vector1;
    f1,f2:text;
function root(x:longint):longint;
begin
while x<>tata[x] do x:=tata[x];
root:=x;
end;
procedure uneste(x,y:longint);
begin
if sz[x]>sz[y] then tata[y]:=x else tata[x]:=y;
if sz[x]=sz[y] then sz[y]:=sz[y]+1;
end;
begin
assign (f1,'disjoint.in');
assign (f2,'disjoint.out');
reset (f1);
rewrite (f2);
readln (f1,n,m);
for i:=1 to n do begin tata[i]:=i; sz[i]:=0; end;
for i:=1 to m do begin
readln (f1,tip,x,y);
if tip=1 then uneste(root(x),root(y)) else
if tip=2 then begin
if root(x)=root(y) then writeln (f2,'DA') else writeln (f2,'NU');
end;
end;
close (f1);
close (f2);
end.