#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
const int N = 2e6 + 7;
int fa[N];
int n,m;
void init(int n) {
for(int i = 1; i <= n; i++)
fa[i] = i;
}
int find(int x) {
if(fa[x] == x) return x;
return fa[x] = find(fa[x]);
}
void unions(int x,int y) {
fa[find(x)] = find(y);
}
int main( ) {
IOS;
cin >> n >> m;
init(n);
while(m--) {
int a,b;
cin >> a >> b;
unions(a,b);
}
int q;
cin >> q;
while(q--) {
int x,y;
cin >> x >> y;
if(find(x) == find(y))
cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
数据结构--并查集
最新推荐文章于 2024-07-18 13:20:33 发布