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.

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)

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

4

1 2

3 4

5 6

1 6

4

1 2

3 4

5 6

7 8

4

2

解:这个题是一个并查集的题,标记的红色字体是题目的关键,他只想留住有朋友关系的人。但是当n等于0时,也就是所有的人都互不认识的时候,就随机找一个人留下,当时这一点没有看到。当时看题目以为是如果所有人中的若干个人是朋友关系,就选出任意两个;如果一个人与其他的人无关系就让这个人留下,结果看到给出的例子纠结了好久,不知道哪里错了。正确的是合并两个人的时候判断一下两个人是否已经直接或者间接有朋友关系,再合并一下这个集合中所包含元素的个数,找出最大的集合人数。所以做题的时候不能看到题目想当然,结合数据理解题意。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const
int maxn=;
int
pre[maxn],num[maxn];
void
creat()
{

for
(int i=;i<maxn;i++)
{

pre[i]=i;
num[i]=;
}
}

int
findroot(int root)
{

if
(root==pre[root])
return
root;
pre[root]=findroot(pre[root]);
return
pre[root];
}

int
main()
{

int
t,n,m,maxx;
while
(scanf("%d",&t)!=EOF)
{

if
(t==)
{

puts("1");
continue
;
}

maxx=-;
creat();
while
(t--)
{

scanf("%d %d",&n,&m);
int
root1=findroot(n);
int
root2=findroot(m);
if
(root1!=root2)
{

if
(root1<root2)
swap(root1,root2);
pre[root2]=root1;
num[root1]+=num[root2];
maxx=max(maxx,num[root1]);
}
}

printf("%d\n",maxx);
}

return
;
}

hdu1856的更多相关文章

  1. 【HDU1856】More is better(并查集基础题)

    裸并查集,但有二坑: 1.需要路径压缩,不写的话会TLE 2.根据题目大意,如果0组男孩合作的话,应该最大的子集元素数目为1.所以res初始化为1即可. #include <iostream&g ...

  2. hdu-1856 More is better---带权并查集

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1856 题目大意: 一个并查集 计算每个集合的元素 找出元素最多的那个集合,输出元素的个数 解题思路: ...

  3. hdu1856 More is better 基础并查集

    #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> ...

  4. hdu1856 选出更多的孩子

    题目大意: 老师选取2个学生对应的号码,这两人视作朋友,同时朋友的朋友也可以看成自己的朋友. 最后老师选出一个人数最多的朋友圈. 这里学生的人数不大于10^7,所以操作时需要极为注意,操作步数能省则省 ...

  5. hdu1856 More is better (并查集)

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  6. hdu1856 并查集

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1856/ 题目就是要求并查集中各树的大小的最大值,我们只要在根节点处存树的大小就可以,合并也是合并根节点的数,最后 ...

  7. 并查集(Union-Find) 应用举例 --- 基础篇

    本文是作为上一篇文章 <并查集算法原理和改进> 的后续,焦点主要集中在一些并查集的应用上.材料主要是取自POJ,HDOJ上的一些算法练习题. 首先还是回顾和总结一下关于并查集的几个关键点: ...

随机推荐

  1. centos6删除mysql安装

    1.查看已经安装了的mysql包: 2.卸载mysql: 3.查看剩下的mysql安装包: 4.逐个删除剩下的mysql安装包: 5.删除完后再次查看,以确保已删除干净: 6.删除自己安装的mysql ...

  2. [Offer收割] 编程练习赛1

    A HihoCoder 1268 九宫 思路: 一般类似于数独的题目都是使用回溯解决,这里由于题目数据较小同样可以直接DFS得出结果.这里我用了一个偷懒的方法(next_permutation),直接 ...

  3. js 实现json数组集合去重,差集,并集,交集。

    let list = [ { id: "1", content: "A" }, { id: "2", content: "B&qu ...

  4. Gatling实战(二)

    在上一篇实战讲解了Gatling的用例,不过还没涉及到性能方面的内容,其实用例中的最后一句就和性能有关了 setUp(scn.inject(atOnceUsers(1)).protocols(http ...

  5. spring-IoC的配置文件applicationContext.XML

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  6. python 环境安装和卸载1

    同事换了新机器,系统从win7变为了win10,因此需要重新安装python环境啦!不废话,直奔主题. A 安装 一.进入python官网  www.python.org 进入downloads-&g ...

  7. windows修改注册表添加开启自启动

    快捷键win+R regedit 计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 新建字符串值 C:\soft ...

  8. 工作流Activiti入门教程

    https://blog.csdn.net/chenweifu365/article/details/79032758/

  9. Mybatis Generator代码自动生成(实体类、dao层、映射文件)

    写了一段时间增删改查有点厌烦,自己找了下网上的例子鼓捣了下自动生成. 首先得有一个配置文件: generatorConfig.xml <?xml version="1.0" ...

  10. Maven解决包冲突

    依赖树 $ mvn dependency:tree [WARNING] [WARNING] Some problems were encountered while building the effe ...