Cod sursa(job #1622572)

Utilizator mirelabocsabocsa mirela mirelabocsa Data 1 martie 2016 12:26:37
Problema Paduri de multimi disjuncte Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.81 kb
program mire;
var f,g:text;
     bufin,bufout:array[1.. 1 shl 16] of byte;
     n,m,c,x,y,i,z,r:longint;
    t:array[1..100000] of longint;
function rad(nod:longint):longint;
begin
 if t[nod]<>0 then
  rad:=rad(t[nod])
  else
 rad:=nod;
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 m do
    begin
       readln(f,c,x,y);
       if c=1 then
          begin
             r:=rad(x);
             z:=rad(y);
             t[z]:=r;
          end
       else
         begin
            if rad(x)=rad(y) then
               writeln(g,'DA')
             ELSE
                writeln(g,'NU')
         end;

    end;
    //for i:=1 to n do
      //write(g,t[i],' ');
close(f);
close(g);
end.