`
Simone_chou
  • 浏览: 192835 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

More is better(并查集)

    博客分类:
  • HDOJ
 
阅读更多

 

More is better

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

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.

    题意:

    给出n种朋友关系,在这么多堆朋友圈中,输出最多人数一组的人数。

    思路:

    并查集。增加一个数组rank来统计某个节点下面有多少人,合并的时候累加一层即可。

    AC:

#include<stdio.h>
#define max 10000000
int root[max],rank[max];
int find(int a)
{
	int x=a,t;
	while(root[x]!=x)
	  x=root[x];
	while(x!=a)
	 {
	 	t=root[a];
	 	root[a]=x;
	 	a=t;
	 }
	return x;
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int a,b,m=0,sum=1;
		for(int i=1;i<max;i++)
		 {
		 	root[i]=i;
		 	rank[i]=1;
		 }
		while(n--)
		{
			int fa,fb;
			scanf("%d%d",&a,&b);
			fa=find(a);
			fb=find(b);
			if(a>sum) sum=a;
			if(b>sum) sum=b;
			if(fa==fb) continue;
			else
			{
				rank[fb]+=rank[fa];
//只需要增加这一句,来求叠加人数
				root[fa]=fb;
			}
			
		}
		for(int i=1;i<=sum;i++)
		  if(rank[i]>m) m=rank[i];
//查找rank中最大值即为所求
		printf("%d\n",m);
	}
	return 0;
}

   总结:

   看清题目,不要钻牛角尖。

分享到:
评论

相关推荐

    Is it better to enjoy your money when you earn it or is it be

    本文将探讨这个主题,并将其与IT行业的实践相结合,来阐述为什么储蓄至少部分额外收入对于未来是更有益的。 首先,储蓄能为个人提供财务安全。在IT行业中,技术更新迅速,竞争激烈,即使是一名高薪的程序员或项目...

    equal risk bounding is better than risk parity for portfolio selection.pdf

    然而,根据《Equal Risk Bounding is better than Risk Parity for portfolio selection》的研究,风险平价方法在理论上被一种名为“平等风险边界”(Equal Risk Bounding,ERB)的方法所超越。 ERB策略并不强制...

    Apixaban or enoxaparin_ Which is better forthromboprophylaxis af

    Apixaban or enoxaparin_ Which is better forthromboprophylaxis after total hip a

    BetterAndBetter最好用的手势软件

    标题:“BetterAndBetter最好用的手势软件” 在苹果Mac系统中,高效便捷的交互体验一直是用户追求的目标,而“BetterAndBetter”恰好满足了这一需求。它是一款专为Mac用户设计的触控板和鼠标手势软件,极大地提升了...

    ImagesBy360.zip

    Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. ...

    单元测试框架.txt

    Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. ...

    The Gourmet iOS Developer's Cookbook Even More Recipes for Better iOS App azw3

    The Gourmet iOS Developer's Cookbook Even More Recipes for Better iOS App Development 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    betterandbetter

    5. **配置和编译**:接下来运行`./configure`,这个脚本会检查你的系统并配置扩展。如果一切顺利,你可以接着运行`make`进行编译,然后执行`make install`将编译好的扩展安装到PHP的扩展目录。 6. **更新配置**:...

    better-scroll.js

    better-scroll.js

    实验一:Python程序基础练习.doc

    4、有一个字符串“Doing is better than saying”,编写程序对该字符串按照空格进行拆分,再对拆分后的结果进行合并连接成字符串。 5、“dsfs.c.asdf.123@126.comasfdsd.asf@qq.comasdf.sd.sadffds@163.com”提取这...

    better-player播放器(自定义SDK+自定义界面)

    **Better Player 播放器:自定义SDK与自定义界面详解** 在数字媒体领域,一个强大且可定制的播放器是至关重要的。Better Player就是这样一款解决方案,它提供了丰富的功能和高度的灵活性,允许开发者根据需求自定义...

    More Python Programming for the Absolute Beginner

    What better way is there to learn a programming language than with a game-oriented approach? If you ask the many readers that have made this book's prequel, PYTHON PROGRAMMING FOR THE ABSOLUTE ...

    vue使用better-scroll实现菜单列表左右联动

    此外,如果菜单和内容列表的大小是动态的,你还需要在窗口尺寸变化或数据更新时重新计算宽度并更新 Better-Scroll 实例的配置。Vue 提供了 `updated` 生命周期钩子,你可以在其中处理这些情况。 最后,对于更复杂的...

    Better Deep Learning by Jason Brownlee

    Better Deep Learning Train Faster, Reduce Overfitting, and Make Better Predictions by Jason Brownlee 26 step-by-step lessons, 575 pages. quotes from papers and books. step-by-step tutorial projects. ...

    Better JPEG.rar

    《更优的JPEG处理工具——Better JPEG》 在数字化时代,JPEG(Joint Photographic Experts Group)格式因其良好的压缩效率和广泛支持,成为了我们日常生活中最常见的图片格式之一。然而,处理JPEG图片时,如何做到...

    better-comments_visualstudio2010_better-comments_BetterComments_

    标题 "better-comments_visualstudio2010_better-comments_BetterComments_" 暗示我们讨论的是一个名为 "BetterComments" 的插件,该插件专为Visual Studio 2010设计,旨在改进代码注释的体验。"better-comments" 是...

    Better And Better 16.47——【一款超强的触摸板、鼠标、键盘快捷手势+截图+剪切板管理+自动输入法等功能,装机必备】

    Better And Better mac版是一款集合众多优秀功能的Mac手势神器,可以帮助用户自定义的设置mac触控板所支持的手势操作,并且还支持单个应用的手势操作。BetterAndBetter Mac版除了手势快捷设定和操作以外,还兼有显示...

    obsidian-better-export-pdf-1.0.5.zip

    值得注意的是,尽管这款插件增强了PDF导出的灵活性,但并不是所有用户都需要这些高级功能。对于只需要基础导出的用户,Obsidian的原生功能可能已经足够。因此,在决定是否安装此插件时,应根据个人需求来权衡。 综...

    better wmf

    Better WMF能自动识别CAD图形的边界,确保只选择并复制需要的部分,避免不必要的背景或者其他元素被一同导入。这大大提高了工作效率,减少了手动编辑的繁琐步骤。 更进一步,Better WMF还提供了对比和优化功能。...

Global site tag (gtag.js) - Google Analytics