最好不要用getchar
by duoluoluo @ 2022-09-02 12:46:35
```cpp
#include <bits/stdc++.h>
using namespace std;
int f[30005],T,num[30005],front[30005];
const int N=30000;
void inxt(){
for(int i=1;i<=N;i++){
f[i]=i;
num[i]=1;
front[i]=0;
}
}
int getf(int x){
int fn=x;
if(f[x]!=x) fn=getf(f[x]);
front[x]+=front[f[x]];
f[x]=fn;
return f[x];
}
void gether(int x,int y){
x=getf(x);
y=getf(y);
if(x!=y){
f[x]=y;
front[x]=num[y];
num[y]+=num[x];
num[x]=0;
}
}
int main(){
scanf("%d",&T);
inxt();
for(int i=1;i<=T;i++){
getchar();
int x,y;
char c;
scanf(" %c %d%d",&c,&x,&y);
if(c=='M')
gether(x,y);
else if(c=='C'){
if(getf(x)!=getf(y))
printf("-1\n");
else
printf("%d\n",abs(front[x]-front[y])-1);
}
}
return 0;
}
```
改成这样就过了
by duoluoluo @ 2022-09-02 12:47:52
这样 `while (c != 'M' && c != 'C')
c = getchar();`
by Galex @ 2022-09-02 12:49:21
@[Galex](/user/483037) 找到一个头像和我一样的大佬
by Kkfrqwfvqb @ 2022-09-03 15:48:11
排名93诶…tql orz
by Kkfrqwfvqb @ 2022-09-03 15:48:38