Cod sursa(job #343743)

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