Cod sursa(job #305174)

Utilizator SprzlAbcdefg Sprzl Data 16 aprilie 2009 15:09:29
Problema Paduri de multimi disjuncte Scor 80
Compilator fpc Status done
Runda Arhiva educationala Marime 1.06 kb
program disjoint;
type  nod = ^articol;
      articol = record
              root:nod;
              l:integer;
            end;
var i,p,q,n,m:longint;
    c:byte;
    a:array [1..100000] of nod;


function find(var nd:nod):nod;
begin
  if nd^.root = nd then
    find:=nd
  else
  begin
    nd^.root:=find(nd^.root);
    find:=nd^.root;
  end;
end;
procedure union(m1,m2:nod);
var aux1,aux2:nod;
begin
  aux1:=find(m1);
  aux2:=find(m2);

  aux1^.root:=aux2;
  aux2^.l:=aux2^.l+aux1^.l;
end;
begin
  assign(input,'disjoint.in');
  assign(output,'disjoint.out');
  reset(input);
  rewrite(output);
  readln(n,m);
  for i:=1 to n do
  begin
    new(a[i]);
    a[i]^.root:=a[i];
    a[i]^.l:=1;
  end;

  for i:=1 to m do
  begin
    readln(c,p,q);
    if c =  1 then
    begin
      if a[p]^.l<a[q]^.l then
        union(a[p],a[q])
      else
        union(a[q],a[p]);
    end
    else
      if find(a[q]) = find(a[p]) then
        writeln('DA')
      else
        writeln('NU');
  end;

  close(input);
  close(output);

end.