杭电 hdu 1856 More is better (并查集)

杭电 hdu 1856  More is better  (并查集)



More is better

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 18187    Accepted Submission(s): 6700


Problem Description
Mr Wang wants some boys to help him with a project. Because the project is rather complex,  the more boys come, the better it will be. Of course there are certain requirements.

Mr Wang selected a room big enough to hold the boys. The boy who are not been chosen has to leave the room immediately. There are 10000000 boys in the room numbered from 1 to 10000000 at the very beginning. After Mr Wang's selection any two of them who are still in this room should be friends (direct or indirect), or there is only one boy left. Given all the direct friend-pairs, you should decide the best way.
 

Input
The first line of the input contains an integer n (0 ≤ n ≤ 100 000) - the number of direct friend-pairs. The following n lines each contains a pair of numbers A and B separated by a single space that suggests A and B are direct friends. (A ≠ B, 1 ≤ A, B ≤ 10000000)
 

Output
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep. 
 

Sample Input
 
   
4 1 2 3 4 5 6 1 6 4 1 2 3 4 5 6 7 8
 

Sample Output
 
   
4 2
Hint
A and B are friends(direct or indirect), B and C are friends(direct or indirect), then A and C are also friends(indirect). In the first sample {1,2,5,6} is the result. In the second sample {1,2},{3,4},{5,6},{7,8} are four kinds of answers.
 

Author
lxlcrystal@TJU
 

Source
HDU 2007 Programming Contest - Final
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1879  1863  1102  1162  1301 
 










题意:就是有很多不同类型的人,但是有很多的人是相互认识的,同时,朋友的朋友也就是你的朋友
a认识b,b认识c,那么a,b,c就算都认识
然后就是问你这样的朋友圈,最多有多少人
输入的首行是n,表示下面有多少对认识的朋友
接下来是a,b,表示a,b相互认识






题解:通过并查集,将认识的归为一类,接着找出最多人数的那一堆就好了







#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

const int maxn = 300005;

int father[maxn];              //记录每个点的祖先结点
int cont[maxn];                 //记录这个点所在的树的最多结点数
int sum;

void Init (){              
	int i;

	for (i = 1; i < maxn; ++i){                  //初始化,先将每个点的父亲结点设置为自己
		father[i] = i;
		cont[i] = 1;
	}
}

int Find (int x){
	int rt = x;

	if (father[x] != x){                      //通过递归找到点的祖先结点
		rt = Find (father[x]);
		father[x] = rt;                       //剪纸,通过寻找祖先结点时就将父亲结点覆盖为祖先结点
	}
	return rt;
}

void Union(int a, int b){
	int fa = Find (a);
	int fb = Find (b);

	if (fa != fb){ 
		if (cont[fa] > cont[fb]){              //将小棵的树合并到大的树里面
			father[fb] = fa;
			cont[fa] += cont[fb];
			if (cont[fa] > sum)                  //查找最多结点数的结点数
				sum = cont[fa]; 
		}
		else {
			father[fa] = fb;
			cont[fb] += cont[fa];
			if (cont[fb] > sum)
				sum = cont[fb];
		}
	}
}

int main (){
	int n, x, y, i, a, b;

	while (scanf ("%d", &n) != EOF){
		if (n == 0){
			printf("1\n");
			continue;
		}
		Init ();
		mapmp;
		int num = 1;
		sum = 0;
		for (i = 1; i <= n; ++i){
			scanf ("%d%d", &x, &y);
			if (mp.find(x) != mp.end())                //个人感觉给出的10000000太大了,要是都放到数组中,容易爆内存
				a = mp[x];                             //而n只有100000,那么输入最多的朋友,最多也就200000,就开了map,将他们放到map中,减少量
			else{
				a = num;
				mp[x] = num++;
			}
			if (mp.find(y) != mp.end())
				b = mp[y];
			else{
				b = num;
				mp[y] = num++;
			}
			Union(a, b);
		}
		printf ("%d\n", sum);
	}

	return 0;
}


你可能感兴趣的:(并查集)