计蒜客练习题:朋友
题目
样例
代码
#include<iostream>
using namespace std;
int father[5005],n,m,p,a,b;
void init()
{
for(int i=1;i<=n;++i)
father[i]=i;
}
int get(int x)
{
if(father[x] == x) return x;
return father[x]=get(father[x]);
}
void merge(int x,int y)
{
x=get(x);
y=get(y);
if(x!=y) father[y] = x;
}
int main()
{
cin>>n>>m>>p;
init();
while(m--)
{
cin>>a>>b;
merge(a,b);
}
while(p--)
{
cin>>a>>b;
if(get(a)==get(b)) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}