#include <iostream>
using namespace std;
const int N = 1e5;
int p[N];
int Size[N];
int Find(int x)
{
if (x != p[x])
{
p[x] = Find(p[x]);
}
return p[x];
}
void Merge(int a, int b)
{
if (Find(a) != Find(b))
{
Size[Find(b)] += Size[Find(a)];
p[Find(a)] = p[Find(b)];
}
}
int main()
{
int m, n;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
p[i] = i;
Size[i] = 1;
}//并查集初始化,使得每个点都为独立的一个集合
int a, b;
string op;
while (m--)
{
cin >> op;
if ("C" == op)
{
cin >> a >> b;
Merge(a, b);
}
else if("Q1" ==op)
{
cin >> a >> b;
if (Find(a) == Find(b))
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
}
else
{
cin >> a;
cout << Size[Find(a)] << endl;
}
}
return 0;
}
//
Size[Find(b)] += Size[Find(a)];
p[Find(a)] = p[Find(b)];
注意这两句不要写反,不然就成先合并后计算了实际上成了只运算b的连通图上的点的数量