1 条题解
-
0
Pascal :
var f:array [1..30000] of integer; b,c:array [1..30000] of integer; t:longint; ch:char; i,j,p,q:integer; k:longint; function find(x:integer):integer; begin if f[x]=x then exit(x); find:=find(f[x]); c[x]:=c[x]+c[f[x]]-1; f[x]:=find; end; begin for i:=1 to 30000 do begin f[i]:=i; b[i]:=1; c[i]:=1; end; // assign(input,'galaxy.in'); assign(output,'galaxy.out'); // reset(input); rewrite(output); readln(t); for k:=1 to t do begin readln(ch,i,j); p:=find(i); q:=find(j); case ch of 'M':begin f[p]:=q; c[p]:=b[q]+1; b[q]:=b[q]+b[p]; end; 'C':begin if p<>q then writeln(-1) else writeln(abs(c[i]-c[j])-1); end; end; end; // close(input); close(output); end.
- 1
信息
- ID
- 4211
- 时间
- 2000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者