题目描述
这道题有点滚瓜烂熟了
C++ 代码
#include <iostream>
using namespace std;
const int N = 100010;
int q[N];
int find(int x){
if(x != q[x]) q[x] = find(q[x]);
return q[x];
}
int main(){
int n,m;
cin >> n >> m;
for(int i = 1;i <= n;++i){
q[i] = i;
}
while(m--){
char c;
cin >> c;
int a , b;
cin >> a >> b;
if(c == 'M'){
q[find(a)] = q[find(b)];
}else{
cout << (find(a) == find(b)?"Yes" : "No" )<< endl;
}
}
return 0;
}