rt。代码如下:
#include<iostream>
#include<fstream>
#include<deque>
#include<unordered_map>
#include<algorithm>
using namespace std;
template<typename Ty>
class disjoint_set{
private:
std::unordered_map<Ty,int> hash;
std::deque<int> father;
std::deque<Ty> real_to_Ty;
int x;
int inside_get(int realnum){
// std::cout<<"50";
return ((father[realnum]==realnum)?father[realnum]:father[realnum]=inside_get(father[realnum]));
}
int insert(Ty insertnum){
father.push_back(x);
real_to_Ty[x]=insertnum;
hash[insertnum]=x++;
return x-1;
}
public:
int desjoint_set(){
x=1;
return 0;
}
Ty get_father(Ty c){
int num;
if(hash.find(c)==hash.end())
num=insert(c);
else
num=hash[c];
int answer=inside_get(num);
return real_to_Ty[answer];
}
int merge(Ty num1,Ty num2){
father[hash[get_father(num1)]]=hash[get_father(num2)];
return 0;
}
};
int m,c,n;
int a,b;
disjoint_set<int> fa;
int main(){
cin>>n>>m>>c;
for(int i=1; i<=m; i++)
cin>>a>>b,fa.merge(a,b);
for(int i=1; i<=c; i++){
cin>>a>>b;
if(fa.get_father(a)==fa.get_father(b))
cout<<"Yes\n";
else
cout<<"No\n";
}
return 0;
}