#include<cstdio>
#define Max(a,b) (a)>(b)?(a):(b)
int father[10000001],boy[10000001];
int maxboy;
int find(int x)
{
int r=x,tmp;
while(father[x]!=x)
x=father[x];
while(r!=x) //路径压缩
{
tmp=father[r];
father[r]=x;
r=tmp;
}
return x;
}
void Union(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
father[fx]=fy;
boy[fy]+=boy[fx];
maxboy=(maxboy,boy[fy]);
}
}
int main()
{
int m,i,x,y;
while(scanf("%d",&m)!=EOF)
{
maxboy=1;
for(i=0;i<=100010;i++)
{
father[i]=i;
boy[i]=1;
}
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
Union(x,y);
}
printf("%d\n",maxboy);
}
return 0;
}
卡路径压缩的并查集题目-hdu 1856
最新推荐文章于 2022-12-11 18:22:16 发布