标签:
#include<cstdio>
#include<ctime>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m,q,father[20005],x,y,r1,r2;
int find(int x){
while(father[x]!=x)x=father[x];
return x;
}//寻找根结点编号并压缩路径非递归
int find(int x){
if(father[x]!=x)father[x]=find(father[x]);
return father[x];
}//寻找根结点编号并压缩路径递归
void unionn(int r1,int r2){
father[r2]=r1;
}//合并两个集合
void print(int x,int y){
if(find(x)==find(y))printf("Yes\n");
else printf("No\n");
}//判断&输出
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
father[i]=i;
}
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
r1=find(x);
r2=find(y);
if(r1!=r2)unionn(r1,r2);
}
scanf("%d",&q);
for(int i=1;i<=q;i++){
scanf("%d%d",&x,&y);
print(x,y);
}
printf("Time used=%0.2lf\n",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
标签:
原文地址:http://www.cnblogs.com/cdqzfredrick/p/5268329.html