#include <iostream>
using namespace std;
const int M=1000;
int n,m,p[M];
int find(int x){
int r=x;
while(p[r]!=r){
r=p[r];
}
int i=x,t;
while(i!=r){
t=p[i];
p[i]=r;
i=t;
}
return r;
}
void join(int a,int b){
int fx= find(a),fy= find(b);
if(fx!=fy){
p[fx]=fy;
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)p[i]=i;
while(m--){
char ask;
int a,b;
cin>>ask>>a>>b;
if(ask=='M'){
join(a,b);
}else{
if(find(a)== find(b))cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
return 0;
}
#include <iostream>
using namespace std;
const int M=1000;
int p[M];
int find(int x){
if(p[x]!=x)p[x]=find(p[x]);
return p[x];
}
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
p[i]=i;
}
while(m--){
char ask;
int a,b;
cin>>ask>>a>>b;
if(ask=='M'){
p[find(a)]= find(b);
}else{
if(find(a)== find(b))cout<<"YES";
else cout<<"NO";
}
}
return 0;
}