Cod sursa(job #892687)

Utilizator dady95bogdan david dady95 Data 26 februarie 2013 11:14:12
Problema Paduri de multimi disjuncte Scor 10
Compilator fpc Status done
Runda Arhiva educationala Marime 1.2 kb
program info;
var t,h:array[0..100010] of longint;
    n,m:longint;
    f,g:text;

function tata(x:longint):longint;
BEGIN
   if x=t[x] then
      tata:=x
             else
      BEGIN
         t[x]:=tata(t[x]);
         tata:=t[x];
      END;
END;

procedure citire;
var i,op,x,y:longint;
BEGIN
   assign(f,'disjoint.in'); reset(f);
   assign(g,'disjoint.out'); rewrite(g);
   readln(f,n,m);
   for i:=1 to n do
       BEGIN
          t[i]:=i;
          h[i]:=1;
       END;
   for i:=1 to m do
       BEGIN
          readln(f,op,x,y);
          if op=1 then
             BEGIN
                if tata(x)<>tata(y) then
                begin
                   if h[x]>h[y] then
                      t[y]:=x
                                else

                       t[x]:=y;
                   if h[x]=h[y] then
                      h[y]:=h[y]+1;
                end;
             END
                   else
             BEGIN
                if tata(x)=tata(y) then
                   writeln(g,'DA')
                                   else
                   writeln(g,'NU');
             END;
       END;
END;

BEGIN
   citire;
   close(f);
   close(g);
END.