More is better 

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

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.

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <cmath>
using namespace std;
#define Max 10000000+5
int per[Max];
int ans[Max];
int num[Max];
bool vis[Max];
int n;
void init()
{
for(int i=;i<Max;i++)
per[i]=i;
}
int find(int a)
{
if(a==per[a])
return a;
return per[a]=find(per[a]);
}
int unite(int a,int b)
{
a=find(a);
b=find(b);
if(a!=b)
per[a]=b;
return ;
}
int main()
{
int t,a,b;
freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
init();
int Maxn=,p=;
memset(vis,,sizeof(vis));
fill(ans,ans+Max,);
for(int i=;i<n;i++)
{
scanf("%d%d",&a,&b);
if(vis[a]==)
{
vis[a]=;
num[p++]=a;
}
if(vis[b]==)
{
vis[b]=;
num[p++]=b;
}
unite(a,b);
}
for(int i=;i<p;i++)
{
t=find(num[i]);
ans[t]++;
//cout<<t<<endl;
Maxn=max(Maxn,ans[t]);
}
printf("%d\n",Maxn);
}
}
 

More is better(hdu 1856 计算并查集集合中元素个数最多的集合)的更多相关文章

  1. POJ 1703 Find them, Catch them【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】

      The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the ...

  2. HDU1865--More is better(统计并查集的秩(元素个数))

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

  3. HDU 2818 (矢量并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2818 题目大意:每次指定一块砖头,移动砖头所在堆到另一堆.查询指定砖头下面有几块砖头. 解题思路: ...

  4. hdu 1116 欧拉回路+并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 给你一些英文单词,判断所有单词能不能连成一串,类似成语接龙的意思.但是如果有多个重复的单词时,也必须满足这 ...

  5. Bipartite Graph hdu 5313 bitset 并查集 二分图

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5313 题意: 给出n个顶点,m条边,问最多添加多少条边使之构成一个完全二分图 存储结构: bitset   ...

  6. hdu 3081(二分+并查集+最大流||二分图匹配)

    Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  7. 2015 ACM/ICPC Asia Regional Changchun Online HDU - 5441 (离线+并查集)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给你n,m,k,代表n个城市,m条边,k次查询,每次查询输入一个x,然后让你一个城市对(u,v ...

  8. hdu 3536【并查集】

    hdu 3536 题意: 有N个珠子,第i个珠子初始放在第i个城市.有两种操作: T A B:把A珠子所在城市的所有珠子放到B城市.  Q A:输出A珠子所在城市编号,该城市有多少个珠子,该珠子转移了 ...

  9. HDU 1829 分组并查集

    题意:有两种性别,每组数据表示是男女朋友,判断输入的几组数据是否有同性恋 思路:http://blog.csdn.net/iaccepted/article/details/24304087 分组并查 ...

随机推荐

  1. easy ui 实现gridview效果

    前台: // 加载审批步骤列表 function FillStep(flowID) { $('#tbStepList').datagrid({ url: "/System/ApproverS ...

  2. Linux安装oracle报错解决

    安装报错:[oracle@centos1 database]$ ./runInstaller Starting Oracle Universal Installer... Checking insta ...

  3. C51变量的存储

    一.全局变量和局部变量 全局变量和局部变量的区别在于作用域的不同.此外还有静态全局变量和静态局部变量. 全局变量作用域为全局,在一个源文件中定义,其他的源文件也可以应用.在其他的源文件中使用exter ...

  4. 转:关于rename命令ubuntu下的用法

    下面是我的遭遇:上午想批量改几个文件的名字,觉得mv在批量方面不够方便,百度到了rename这个命令,原谅我吧,我总是在百度不到结果时才去看google,以后还是少去百度的好国内很多贴子都在说linu ...

  5. js收集错误信息,错误上报

    线上的代码可有有时候用户会反应不好使,一般是因为js造成的! 尤其在移动端各个手机之前的差异特别大. 下面这段代码是获取能帮助你! <script> window.onerror = fu ...

  6. kibana 统计每天注册数

  7. 2013第49周三IE9文档模式

    今天完善了原有模块的代码和注释,然后继续之前新模块的开发,并写了两边的service接口,除了因为邮件中有部分问题让我分心外,专心下来写代码的感觉真好,今天基本上没遇到多少让我新感悟的技术问题,就总结 ...

  8. 通过expdp/impdp进行oracle数据库的备份恢复详细指导

    假定导出oracle数据库home目录为/opt/oracle,数据库用户为exp_user/test,导入用户为imp_user/test,给出如下样例,具体使用时根据实际情况修改路径及用户名/密码 ...

  9. C++私有构造函数

    一. 类的构造函数一般是public的,但是也可以是private的.构造函数为私有的类有这样的特点: <1>不能实例化:因为实例化时类外部无法访问其内部的私有的构造函数: <2&g ...

  10. linux find命令-print0和xargs中-0使用技巧(转载)

    本文介绍了linux find命令中-print0和xargs中-0用法技巧,一些find命令的使用经验,需要的朋友参考下. 本节内容:linux find命令中-print0和xargs中-0的用法 ...