You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.2 KiB

#include <bits/stdc++.h>
using namespace std;
#pragma region 并查集模板
int p[101]; //存储元素的父节点
int Rank[101]; //存储树的高度(秩)
//建立并查集
void Build(int n) {
for (int i = 1; i <= n; ++i) {
p[i] = i; //父结点是元素自身
}
}
//查找根结点//路径压缩
int Find(int x) {
if (p[x] != x)
p[x] = Find(p[x]);
return p[x];
}
//按秩合并集合
void Union(int x, int y) {
x = Find(x);
y = Find(y);
if (Rank[x] < Rank[y]) p[x] = y;
else {
p[y] = x;
if (Rank[y] == Rank[x]) Rank[x]++;
}
}
#pragma endregion 并查集模板
int main() {
//输入+输出重定向
freopen("../1412.txt", "r", stdin);
int n, m;
cin >> n >> m;
//建立并查集
Build(n);
//合并集合
for (int i = 1; i <= m; ++i) {
int a, b;
cin >> a >> b;
Union(a, b);
}
int q;
cin >> q;
for (int i = 1; i <= q; ++i) {
int c, d;
cin >> c >> d;
if (Find(c) == Find(d)) cout << "Yes" << endl;
else cout << "No" << endl;
}
//关闭文件
fclose(stdin);
return 0;
}