时间限制: 1 Sec 内存限制: 128 MB
题目描述:有一个城镇,住着n个市民。已知一些人互相为朋友。引用一个名人的话说,朋友的朋友也是朋友。意思是说如果A和B是朋友,C和B是朋友,则A和C是朋友.你的任务是数出最大朋友组的人数。输入输入第一行由N,M组成,N是市民的个数(1<=n<=30000),m是朋友对的个数(0<=m<=500000)。下面的m行每一行由两个数A和B组成(1<=A,B<=N,A<>B)表示A和B是朋友。注意给的朋友对可能会有重复。输出输出仅有一行包含一个整数,表示要求的最大朋友组的人数。
样例输入
10 12
1 2
3 1
3 4
5 4
3 5
4 6
5 2
2 1
7 10
1 2
9 10
8 9
样例输出
6
//这道题是练习并查集的基本题目,刚开始学习并查集,我也没真实搞懂,初期就先背过了学长给的代码,能够暂时解决一些题目。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <math.h>
#include <map>
#define ll long long
#define inf 0x3f3f3f3f
#define maxn 100000
using namespace std;
int father[maxn];
int num[maxn];
void init ()
{
for(int i=0; i<=30000; i++)
father[i]=i;
}
int findfather(int x)//寻找祖先
{
if(father[x]==x)
return x;
else
return father[x]=findfather(father[x]);
}
void Union (int x,int y)//结合为朋友
{
int fx=findfather(x);
int fy=findfather(y);
if(fx!=fy)
father[fx]=fy;
}
bool cmp(int a,int b)//排序标准
{
return a>b;
}
int main()
{
int n,m,i;
scanf("%d%d",&n,&m);
int ans=n-1;
int a,b;
init();
while(m--)
{
scanf("%d%d",&a,&b);
Union(a,b);
}
for(i=1; i<=n; i++)
{
num[findfather(i)]++;
}
sort(num,num+maxn,cmp);
printf("%d\n",num[0]);
return 0;
}