Cod sursa(job #892691)

Utilizator dady95bogdan david dady95 Data 26 februarie 2013 11:17:05
Problema Paduri de multimi disjuncte Scor 50
Compilator fpc Status done
Runda Arhiva educationala Marime 1.24 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,x1,y1: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);
          x1:=tata(x);
          y1:=tata(y);
          if op=1 then
             BEGIN
                if x1<>y1 then
                begin
                   if h[x1]>h[y1] then
                      t[y1]:=x1
                                else

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

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