题目描述

题解思路
参考https://www.acwing.com/solution/content/19232/
代码
cpp
#include <iostream>
using namespace std;
const int N = 100010;
int n, m, a, b;
int p[N];
string op;
int find(int x) // 带路径压缩的查询
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i++) p[i] = i;
while (m -- )
{
cin >> op >> a >> b;
if (op == "M") p[find(a)] = find(b); // 合并集合
else
{
if (find(a) == find(b)) puts("Yes");
else puts("No");
}
}
return 0;
}
