Cod sursa(job #934432)

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

function tata (x:longint):longint;
begin
 while x<>t[x] do x:=t[x];
 tata:=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,key,x,y);
  if key=1 then
  begin
   uneste(tata(x), tata(y));
  end
  else
  begin
   if tata(x)=tata(y) then
    writeln (g,'DA')
   else writeln (g,'NU');
  end;
 end;
 close (F); close (G);
end.