HDU1856 - More is better 利用并查集找最大群体数目

HDU1856 - More is better: http://acm.hdu.edu.cn/showproblem.php?pid=1856

题意: a和b认识,b和c认识,则a b c互相认识. 给出一些相互认识的两个人的编号. 判断最多有多少人互相认识(包括自己).

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int MAXN = 100011;
int N;
int x,y;
int MaxId,MaxNum,Num[MAXN],Father[MAXN];
void Initial()
{
	MaxId = MaxNum = -1;
	for(int i = 0;i < MAXN;i++)
		Father[i] = i,Num[i] = 1;//Num数组的元素都初始化为 1,最少有一个人
}
int Find(int x)
{
/*	if(x != Father[x])
		Father[x] = Find(Father[x]);
	return Father[x];
*/
	return x == Father[x] ? Father[x] : Father[x] = Find(Father[x]);
}
void Union(int x,int y)
{
	x = Find(x);
	y = Find(y);
	if(x != y)
		Father[x] = y,Num[y] += Num[x];//合并的同时将人数加到根节点上,这里要注意,不要写反了
}
int main()
{
	while(~scanf("%d",&N))
	{
		if(!N)
		{
			printf("1\n");
			continue;
		}
		Initial();
		while(N--)
		{
			scanf("%d%d",&x,&y);
			//这里求出出现过的id最大为MaxId,因为这里是千万的数量级,全部循环一次会耗费较多的时间
			if(MaxId < x)MaxId = x;
			if(MaxId < y)MaxId = y;
			Union(x,y);
		}
		for(int i = 0;i <= MaxId;i++)//找出最大的人数
			if(MaxNum < Num[i])
				MaxNum = Num[i];
		printf("%d\n",MaxNum);
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 02:47:36

HDU1856 - More is better 利用并查集找最大群体数目的相关文章

利用并查集判断一个无向图是否成树

hdu 1272 利用并查集方法,找到各点的根节点. 几点注意: 一.树:1.无环 2.根节点入度为0,其余入度为1 判断依据: 1.若两个点的根节点相同(这两个点是父子关系),则形成环. 2.若所有点中只有一个点的根节点是他本身,则根节点入度为0. 二. 1. 0 0 :空树也是一颗树. 1 #include<iostream> 2 #include<algorithm> 3 #define Max 100005 4 using namespace std; 5 6 int f[

分别利用并查集,DFS和BFS方法求联通块的数量

联通块是指给定n个点,输入a,b(1<=a,b<=n),然后将a,b连接,凡是连接在一起的所有数就是一个联通块: 题意:第一行输入n,m,分别表示有n个数,有输入m对连接点,以下将要输入m行(输入数据到文件截止): 输出:第一行要求输出联通块的个数,并在第二行分别输出每个联通块中点的数量,每个数之间以一个空格隔开. 样例 15 31 42 53 5输出:2 2 3样列2 9 81 22 33 43 74 54 67 87 9输出: 19 如果不明白的话可以画图试试,最多花半个小时,要是早这样不

杭电1272 并查集找环+判断连通

杭电1272 并查集找环+判断连通 E - E Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1272 Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B

hdu1856 More is better (并查集)

More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 19011    Accepted Submission(s): 6998 Problem Description Mr Wang wants some boys to help him with a project. Because the projec

利用并查集判断一个有向图是否成树

hdu 1325 此题与hdu 1272类似. 1 #include<stdio.h> 2 #include<string.h> 3 #define N 110000 4 int f[N],vis[N]; 5 int flag = 0; 6 int Maxx = 0; 7 int num = 1; 8 int Max(int a,int b) 9 { 10 return a>b?a:b; 11 } 12 void star1() 13 { 14 int i; 15 for(i

畅通工程(并查集找根节点)

畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 203 Accepted Submission(s): 168   Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府“畅通工程”的目标是使全省任何两个城镇间都可以实现交通(但不一定有直接的道路相连,只要互相间

cf246 ENew Reform (并查集找环)

Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing r

1151 - Buy or Build(二进制枚举子集 + 并查集)

这题LRJ书上翻译的有问题,书上说两点之间的cost是两点的欧几里得距离,而题目要求两点的距离是两点欧几里得距离的平方. 其余就没什么好说的了,裸的并查集,需要注意的就是二进制枚举子集的问题. 二进制枚举子集: for(int i = 0 ; i < (1 << s) ; i++){ /*s是集合元素的个数*/ for(int j = 0 ; j < s ; j++){ if(!(s >> j) & 1) continue; else{ } } } 140548

并查集(Disjoint Set)

http://www.cnblogs.com/cyjb/p/UnionFindSets.html http://blog.csdn.net/dm_vincent/article/details/7655764 http://blog.csdn.net/dm_vincent/article/details/7769159 并查集(Union-find Sets)是一种非常精巧而实用的数据结构,它主要用于处理一些不相交集合的合并问题.一些常见的用途有求连通子图.求最小生成树的 Kruskal 算法和