Cod sursa(job #903205)

Utilizator mada0222Tomus Madalina mada0222 Data 1 martie 2013 19:07:48
Problema Paduri de multimi disjuncte Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.95 kb
program sdsd;
var f,g:text;
n,m,x,y,i,z,e1,e2:longint;
r,t:array[1..100000] of longint;
bufin,bufout:array[1..65000] of byte;
Function tata(x:longint):longint;
  begin
    while t[x]<>x do
       x:=t[x];
       tata:=x;
  end;
Procedure uneste(x,y:longint);
  begin
     if r[x]>r[y] then t[y]:=x
     else
     t[x]:=y;
     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
  r[i]:=1;
  t[i]:=i;
  end;
   for i:=1 to m do
     begin
        readln(f,x,y,z);
        e1:=tata(y);
        e2:=tata(z);
          if x=1 then
            uneste(e1,e2)
            else
              begin
                 if e1=e2 then
                    writeln(g,'DA')
                    else
                    writeln(g,'NU');
              end;
     end;
close(f);
close(g);
end.