Cod sursa(job #892657)

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

function tata(x:integer):integer;
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
                   if h[x]>h[y] then
                      t[y]:=t[x]
                                else
                      if h[x]<h[y] then
                         t[x]:=t[y]
                                   else
                         BEGIN
                            t[x]:=t[y];
                            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.