#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<string.h>
#include<sstream>
using namespace std;
int bin[5005];
void init(){
for(int i = 1; i <= 5000; i ++){
bin[i] = i;
}
}
int find(int x){
int r = x, tem;
while(x != bin[x]){
x = bin[x];
}
while(r != x){
//路径压缩
tem = bin[r];
bin[r] = x;
r = tem;
}
return x;
}
int main() {
ios::sync_with_stdio(false);
int n, m, k;
int a, b;
while(cin >> n >> m >> k){
init();
for(int i = 1; i <= m; i ++){
cin >> a >> b;
int x = find(a);
int y = find(b);
if(x != y)bin[x] = y;//并
}
for(int i = 1; i <= k; i ++){
//查找
cin >> a >> b;
int x = find(a);
int y = find(b);
if(x != y)cout << "No" << endl;
else cout << "Yes" << endl;
}
}
return 0;
}
(并查集+路径压缩+查找)
最新推荐文章于 2022-12-11 18:22:16 发布