Cod sursa(job #885721)

Utilizator yngoMarc Paul yngo Data 22 februarie 2013 11:57:39
Problema Paduri de multimi disjuncte Scor 10
Compilator fpc Status done
Runda Arhiva educationala Marime 0.95 kb
program multimi;
var f,g:text;
x,y,z,m,n,i,x1,x2:longint;
t,r:array[1..100001] of longint;
bufin,bufout:array[1..65000] of byte;

 function arb(x:longint):longint;
begin
  if x=t[x] then
    arb:=x else
      begin
        t[x]:=arb(t[x]);
        arb:=t[x];
      end;
end;

procedure uneste(x,y:longint);
  begin
     if (r[x]>r[y]) then begin
        t[y]:=arb(x);
        end
     else
      begin
     t[x]:=arb(y);
     end;
     if r[x]=r[y] then r[y]:=r[y]+1;

  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;
       r[i]:=1;
     end;
for i:=1 to m do
   begin
     readln(f,x,y,z);

        if x=1 then begin
         uneste(y,z);

         end else if t[y]=t[z] then
            writeln(g,'DA') else
            writeln(g,'NU');
   end;
close(f); close(g);
end.