Cod sursa(job #903228)

Utilizator andrei_toaderToader Andrei Sorin andrei_toader Data 1 martie 2013 19:17:40
Problema Paduri de multimi disjuncte Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.84 kb
program disjuncte;
var f,g:text;
    rg,t:array[1..100000] of longint;
    cd,x,y:longint;
    n,m,i:longint;
    bufin,bufout:array[1..65000] of byte;

function gaseste (x:longint):longint;
begin
 while t[x]<>x do
  x:=t[x];
 gaseste:=x;
end;

procedure uneste (x,y:longint);
begin
 if rg[x]<rg[y] then t[x]:=y
 else t[y]:=x;
 if rg[x]=rg[y] then inc(rg[y]);
end;

begin
 assign (f,'disjoint.in'); reset (f);
 assign (g,'disjoint.out'); rewrite (g);
 settextbuf (f,bufin);
 settextbuf (g,bufout);
 readln (f,n,m);
 for i:=1 to n  do
 begin
  t[i]:=i; rg[i]:=1;
 end;
 for i:=1 to m do
 begin
  readln (f,cd,x,y);
  if cd=1 then
  begin
   uneste(gaseste(x), gaseste(y));
  end
  else
  begin
   if gaseste(x)=gaseste(y) then
    writeln (g,'DA')
   else
    writeln (G,'NU');
  end;
 end;
 close (F); close (G);
end.