您的位置: 首页 > 文章 > 并查集 并查集 分类: 文章 • 2025-04-09 11:36:09 #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; }