题解
2023-07-07 12:42:49
发布于:上海
4阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int n,m,s,pre[10010],p1,p2,p3;
int find(int x)
{
 int r=x; 
 while (pre[ r ] != r) 
     r=pre[ r ] ;
  return r ;
}
void join(int x,int y)
{
 int fx=find(x),fy=find(y);
 if(fx!=fy)
      pre[fx]=fy;
}
struct node{
	int f1;
	int f2;
	int t1;
};
int main()
{
    cin>>n>>m>>p1;
    for(int i=1;i<=n;i++){
    	pre[i]=i;
	}
	
    for(int i=0;i<m;i++){
		cin>>p2>>p3; 
    	join(p2,p3);
    }
	for(int j=0;j<p1;j++){
		cin>>p2>>p3;
			if(find(p2)==find(p3)){
				cout<<"Yes"<<endl;
			}else{
				cout<<"No"<<endl;
			}
	} 
    return 0;
}
这里空空如也

有帮助,赞一个