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

Tác giả: flashmt

Ngôn ngữ: Pascal

const fi='';
      fo='';
      maxn=10000;
var cha,nhan:array[1..maxn] of integer;
    n,i,x,y,z,p:longint;

function find(x:longint):longint;
begin
     if x=cha[x] then find:=x
     else
     begin
          cha[x]:=find(cha[x]);
          find:=cha[x];
     end;
end;

procedure hop(x,y:longint);
var a,b:longint;
begin
     a:=find(x); b:=find(y);
     if nhan[a]>nhan[b] then cha[b]:=a
     else
     begin
          if nhan[a]<nhan[b] then cha[a]:=b
          else
          begin
               if a<>b then
               begin
                    cha[b]:=a;
                    nhan[a]:=nhan[a]+1;
               end;
          end;
     end;
end;

begin
     assign(input,fi); reset(input);
     assign(output,fo); rewrite(output);
     for i:=1 to maxn do cha[i]:=i;
     readln(p);
     for i:=1 to p do
     begin
          readln(x,y,z);
          if z=1 then hop(x,y)
          else
              if find(x)=find(y) then writeln(1) else writeln(0);
     end;
     close(input); close(output);
end.

Download