Cod sursa(job #343747)

Utilizator ionutz32Ilie Ionut ionutz32 Data 27 august 2009 09:42:54
Problema Paduri de multimi disjuncte Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.67 kb
var t:array[1..100000] of longint;
n,m,i,cod,x,y,s1,s2,aux:longint;
f,g:text;
function s(el:longint):longint;
         begin
         if t[el]=el then
            s:=el
         else
             s:=s(t[el]);
         end;
procedure unire(a,b:longint);
          begin
          t[a]:=b;
          end;
begin
assign(f,'disjoint.in');
assign(g,'disjoint.out');
reset(f);rewrite(g);
readln(f,n,m);
for i:=1 to n do
    t[i]:=i;
for i:=1 to m do
    begin
    readln(f,cod,x,y);
    if cod=1 then
       unire(s(x),s(y))
    else
        if s(x)=s(y) then
           writeln(g,'DA')
        else
            writeln(g,'NU');
    end;
close(f);close(g);
end.