Cod sursa(job #305183)

Utilizator SprzlAbcdefg Sprzl Data 16 aprilie 2009 15:28:14
Problema Paduri de multimi disjuncte Scor 90
Compilator fpc Status done
Runda Arhiva educationala Marime 0.93 kb
program disjoint;
var root,sz:array [1..100000] of longint;
    aux1,aux2,n,m,p,q,c,i:longint;



function find(nod:longint):longint;
begin
  if nod = root[nod] then
    find:=nod
  else
  begin
    root[nod]:=find(root[nod]);
    find:=root[nod];
  end;
end;

procedure union(m1,m2:longint);
begin
  aux1:=find(m1);
  aux2:=find(m2);
  root[aux1]:=aux2;
  if sz[aux1] = sz[aux2] then
    inc(sz[aux2]);
end;

begin
  assign(input,'disjoint.in');
  assign(output,'disjoint.out');
  reset(input);
  rewrite(output);

  readln(n,m);
  for i:=1 to n do
  begin
    root[i]:=i;
    sz[i]:=1;
  end;

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

  close(input);
  close(output);
end.