博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1856:More is better(并查集)
阅读量:6798 次
发布时间:2019-06-26

本文共 2347 字,大约阅读时间需要 7 分钟。

More is better

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


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
并查集入门
# include 
# include
# define MAXN 10000000using namespace std;int pre[MAXN+1], num[MAXN+1];void init(){ for(int i=1; i<=MAXN; ++i) { pre[i] = i; num[i] = 1; }}int find(int x){ if(x != pre[x]) pre[x] = find(pre[x]); return pre[x];}int main(){ int n, x, y, imax; while(~scanf("%d",&n)) { if(n==0) { puts("1"); continue; } init(); imax = -1; while(n--) { scanf("%d%d",&x,&y); int px = find(x); int py = find(y); if(px != py) { pre[px] = py; num[py] += num[px]; imax = max(imax, num[py]); } } printf("%d\n",imax); } return 0;}

转载于:https://www.cnblogs.com/junior19/p/6730018.html

你可能感兴趣的文章
ubuntu 10.04 install oracle11g
查看>>
我的Windows 8下看漫画程序差不多可以用了
查看>>
rabbitmq使用__python客户端(消息接收者)
查看>>
如何实现一套鼠标键盘控制二台主机
查看>>
html5 手机页面
查看>>
【Java学习笔记】Java中关于tostring方法的误操作
查看>>
Ubuntu 配置VNC以及使用VNC连接时,无法显示系统菜单栏,解决方法
查看>>
开发人员应该对IIS理论层的知识了解的多一些~目录
查看>>
linux命令备份
查看>>
Lingo软件的使用
查看>>
【转】CSS经验分享:如何书写可维护的CSS代码
查看>>
substr(),mb_substr()及mb_strcut的区别和用法
查看>>
AS3.0中的反射
查看>>
lynn trigger
查看>>
读书笔记:C++中利用智能指针和值型类防止内存非法访问
查看>>
关于“分叉/联接方案”的一般做法
查看>>
c# 如何通过反射 获取\设置属性值、
查看>>
FIS源码解析之use
查看>>
[zz]cocos2d-x如何优化内存的应用
查看>>
分享:Apache OpenNLP 1.5.3 发布
查看>>