解题思路:将给出的男孩的关系合并后,另用一个数组a记录以find(i)为根节点的元素的个数,最后找出数组a的最大值

More is better

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

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
#include<stdio.h>
#include<string.h>
int pre[10000010],a[10000010];
int find( int root)
{
if(root!=pre[root])
pre[root]=find(pre[root]);
return pre[root];
}
void unionroot( int root1,int root2)
{
int x,y;
x=find(root1);
y=find(root2);
if(x!=y)
pre[x]=y;
} int main()
{
int n;
int root1,root2,x,y,k,i,max;
while(scanf("%d",&n)!=EOF)
{
max=-100000;
for(i=1;i<=10000010;i++)
a[i]=0; for(i=1;i<=10000010;i++)
pre[i]=i;
while(n--)
{
scanf("%d %d",&root1,&root2);
x=find(root1);
y=find(root2);
unionroot(x,y);
} for(i=1;i<=10000010;i++)
{
k=find(i);
a[k]++;//记录以find(i)为根节点的包含有多少 个元素
}
for(i=1;i<=10000010;i++)
{
if(a[i]>max)
max=a[i];
}
printf("%d\n",max);
}
}

  

HDU 1856 More is better【并查集】的更多相关文章

  1. HDU 1856 More is better (并查集)

    题意: 给你两个数代表这两个人是朋友,朋友的朋友还是朋友~~,问这些人组成的集合里面人最多的是多少... 思路: 属于并查集了,我用的是带路径压缩的,一个集合里面所有元素(除了根节点)的父节点都是根节 ...

  2. HDU 1856 More is better(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...

  3. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...

  4. hdu 1257 小希的迷宫 并查集

    小希的迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...

  5. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

  6. <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)

     本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...

  7. HDU 4496 D-City(逆向并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=4496 题意: 给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数. 思路: 离线处理删边,从后 ...

  8. HDU 3407.Zjnu Stadium 加权并查集

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  10. HDU 4641 K-string 后缀自动机 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=4641 https://blog.csdn.net/asdfgh0308/article/details/4096 ...

随机推荐

  1. Declarative programming-声明式编程-布局约束是一个案例

    声明式编程需要底层或运行时环境支持. 声明式语言的关键词确定了执行的关键控制流. 表述编程语言是说明性的东西:而不是具体的执行方案. 通常他的执行由解释器进行. In computer science ...

  2. 鼠标悬浮触发事件(onmouseover)实现

    将鼠标移至(悬浮)到某个标签范围内触发事件或提示消息等效果实现的关键词为:onmouseover. 代码: <!DOCTYPE html> <html> <head> ...

  3. BZOJ 4229: 选择 LCT_独创方法_边双

    考虑如果两点在一个环中,那么这两点一定可以构出双联通分量. 考虑环和环镶嵌,那么两个环中的点一定都互为双联通分量. 由此,我们想到一个算法: 将删边转为反向加边,用LCT维护图. 当我们连接两个点时, ...

  4. Java 面向对象详解

    0 引言 接触项目开发也有很长一段时间了,最近开始萌发出想回过头来写写以前学过的基础知识的想法. 1 面向对象 面向对象(Object Oriented)是一种新兴的程序设计方法,或者是一种新的程序设 ...

  5. 05 ADO.net

    ADO.NET就是一组类库 操作数据库用的.

  6. Jmeter--Timer设置等待时间

    一.Jmeter定时器的概念:1)定时器是在每个sampler(采样器)之前执行的,而不是之后:是的,你没有看错,不管这个定时器的位置放在sampler之后,还是之下,它都在sampler之前得到执行 ...

  7. [NOIP补坑计划]NOIP2015 题解&做题心得

    感觉从15年开始noip就变难了?(虽然自己都做出来了……) 场上预计得分:100+100+60~100+100+100+100=560~600(省一分数线365) 题解: D1T1 神奇的幻方 题面 ...

  8. Vue文件封装日历组件

    封装就是要具有灵活性,样式自适应,调用的时候传入props就可以变成自己想要的样式. 效果展示网址:https://1963331542.github.io/ 源代码: <template> ...

  9. 判断浏览器是PC设备还是移动设备

    var browser={ versions:function(){ var u = navigator.userAgent, app = navigator.appVersion; return { ...

  10. 九、frp对外提供简单的文件访问服务

    通过 static_file 插件可以对外提供一个简单的基于 HTTP 的文件访问服务.类似于http的文件索引! 服务端frps.ini配置[common]bind_addr = 0.0.0.0bi ...