IOIBIN - Các thùng nước

Tác giả: RR

Ngôn ngữ: Pascal

var
  lab:array[1..10111] of longint;
  i,m:longint;
  x,y,q:longint;

function getRoot(u:longint):longint;
    begin
      if lab[u]<0 then exit(u);
      lab[u]:=getRoot(lab[u]); exit(lab[u]);
    end;

procedure union(r1,r2:longint);
    var
      x:longint;
    begin
      if r1=r2 then exit;
      x:=lab[r1]+lab[r2];
      if lab[r1]<lab[r2] then
        begin
          lab[r1]:=x;
          lab[r2]:=r1;
        end
      else
        begin
          lab[r2]:=x;
          lab[r1]:=r2;
        end;
    end;

begin
  for x:=1 to 10000 do lab[x]:=-1;
  read(m);
  for i:=1 to m do
    begin
      read(x,y,q);
      x:=getRoot(x);
      y:=getRoot(y);
      if q=1 then union(x,y)
      else if x=y then writeln(1)
      else writeln(0);
    end;
end.

Download