宋壬初吧 关注:25贴子:419
  • 0回复贴,共1

VIJOS1443

收藏回复

  • 221.194.73.*
//SRC is locked
var i,l,r,t,cccum:longint;
  ltk,w,size:array[1..30000] of longint;
  c:char;
//SRC is locked
function f(x:longint):longint;
var s:longint;
begin
  if ltk[x]=x then exit(x);
  s:=ltk[x];
  ltk[x]:=f(ltk[x]);
  w[x]:=w[x]+w[s];
  exit(ltk[x]);
end;
//SRC is locked
procedure process_1;
begin
  readln(l,r);
  w[f(l)]:=size[f(r)];
  size[ltk[r]]:=size[ltk[r]]+size[ltk[l]];
  ltk[ltk[l]]:=ltk[r];
end;
//SRC is locked
procedure process_2;
begin
  inc(cccum);
  readln(l,r);
  if f(l)=f(r) then write(abs(w[l]-w[r])-1) else write(-1);
  if cccum mod 10000=0 then writeln;
end;
//SRC is locked
begin
  for i:=1 to 30000 do
  begin
    ltk[i]:=i;size[i]:=1;
  end;
  readln(t);
  cccum:=0;
  for i:=1 to t do
  begin
    read©;
    if c='M' then process_1;
    if c='C' then process_2;
  end;
end.
//SRC is locked


1楼2009-10-08 07:29回复