并查集

并查集#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PI 3.141593
using namespace std;
typedef long long ll;
const ll OIU=1e8+5;
int sets[OIU]={0};
void init(){
for(int i=1;i<=11;i++){
sets[i]=i;
}
}
int getfather(int x){
if(sets[x]==x){
return x;
}else{
return getfather(sets[x]);
}
}
void Uion(int xx,int yy){
int fx=getfather(xx);
int fy=getfather(yy);
if(fx!=fy){
sets[fy]=fx;
}
}
int main()
{
int sum=0;
int x,y;
int n=10;
init();
while(n–){
scanf("%d%d",&x,&y);
Uion(x,y);
}
for(int i=1;i<=11;i++){
if(sets[i]==i){
sum++;
}
}
printf("%d\n",sum);
return 0;
}

并查集
并查集
并查集
并查集