Cod sursa(job #343745)

Utilizator ionutz32Ilie Ionut ionutz32 Data 27 august 2009 09:23:10
Problema Paduri de multimi disjuncte Scor 90
Compilator fpc Status done
Runda Arhiva educationala Marime 1.69 kb
var t,gr:array[1..100000] of longint;
n,m,i,cod,x,y,s1,s2,aux:longint;
f,g:text;
function s(el:longint):longint;
         var str:longint;
         begin
         str:=el;
         while t[str]<>str do
               str:=t[str];
         if cod=2 then
            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]);
          while x<>s1 do
                begin
                aux:=t[x];
                t[x]:=s1;
                x:=aux;
                end;
          while y<>s1 do
                begin
                aux:=t[y];
                t[y]:=s1;
                y:=aux;
                end;
          end
       else
           begin
           t[s1]:=s2;
           inc(gr[s2],gr[s1]);
           while x<>s2 do
                begin
                aux:=t[x];
                t[x]:=s2;
                x:=aux;
                end;
          while y<>s2 do
                begin
                aux:=t[y];
                t[y]:=s2;
                y:=aux;
                end;
           end;
       end
    else
        if s(x)=s(y) then
           writeln(g,'DA')
        else
            writeln(g,'NU');
    end;
close(f);close(g);
end.