Cod sursa(job #269038)

Utilizator philipPhilip philip Data 2 martie 2009 11:30:15
Problema Paduri de multimi disjuncte Scor 100
Compilator fpc Status done
Runda Arhiva educationala Marime 0.92 kb
var f,g:text;
    t:array[1..100000] of longint;
    rang:array[1..100000] of longint;

procedure citire;
  var n,m,i,x,y,op:longint;

  function tata(x:longint):longint;
    var xx,y:longint;
    begin
      xx:=x;
      while t[x]<>x do x:=t[x];
      while t[xx]<>xx do begin                {x in loc de xx}
      y:=t[xx];
      t[xx]:=x;
      xx:=y; end;
    end;
  procedure merge(x,y:longint);
    begin
      if rang[x]>rang[y] then t[y]:=x else t[x]:=y; {creste rang cu unu}
    end;
  begin
    assign(f,'disjoint.in');
    reset(f);
    readln(f,n,m);
    assign(g,'disjoint.out');
    rewrite(g);
    for i:=1 to n do t[i]:=i;
    for i:=1 to m do begin
      readln(f,op,x,y);
      if op=2 then begin
        if tata(x)=tata(y) then writeln(g,'DA') else writeln(g,'NU');
      end else begin
        merge(tata(x),tata(y));
      end;
    end;
    close(g);
  end;

begin
  citire;
end.