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. asp.net mvc4 easyui datagrid 增删改查分页 导出 先上传后导入 NPOI批量导入 导出EXCEL

    效果图 数据库代码 create database CardManage use CardManage create table CardManage ( ID ,) primary key, use ...

  2. Java中的日期处理类

    在Java中可以使用Date类和Calendar类来处理日期 但是Date类很多方法都过时了,推荐使用Canlendar类来处理日期,并对日期的格式化做了介绍.下面的部分将会逐一介绍 Date类 Ja ...

  3. Unity3D使用NGUI做个弹窗

    主要函数:  NGUITools.AddChild(gameobj, prefab);//gameobj为父对象,prefab为要显示的窗体对象预制 然后写个方法,限制出现多个即可

  4. cf B. Resort

    http://codeforces.com/contest/350/problem/B 从旅馆开始倒着找到一个点它的出度>1的位置为止,比较长度大小,找到一个长度最大的即可. #include ...

  5. 一道有关球赛队员分配的C++程序题目

    题目描述: 两个球队进行比赛,各出三人.甲队为a,b,c三人,乙队为x,y,z三人.已经抽签决定比赛名单. 有人向队员打听比赛安排的名单.a说他不和x比,c说他不和x,z比,请编程找出三队赛手的名单. ...

  6. Shell工具:jsondiff.sh

    逻辑很简单,无非就是通过curl在不同的服务器上取得结果集,然后diff即可,不过这里有几点需要注意的地方:首先,JSON就一行,直接 diff会失去意义:其次,JSON中汉字会被编码,不利于查看:另 ...

  7. 【转】notepad++设置字体和字体大小

    原文网址:http://www.aichengxu.com/view/604 今天很多朋友问我怎么设置notepad++的代码字体和代码字体的大小,习惯了editplus的朋友可能会在notepad+ ...

  8. HDU 5428 The Factor (素因数分解)

    题意:给出n个数,问这n个数的乘积中至少有三个因子的最小因子.若不存在这样的因子,则输出 -1: 思路:求出每个数的最小的两个素因数,然后输出其中最小的两个数的乘积. 代码: #include< ...

  9. Working with Numbers in PL/SQL(在PL/SQL中使用数字)

    This article gives you all the information you need in order to begin working with numbers in your P ...

  10. Objective-C(十八、谓语使用及实例说明)——iOS开发基础

    结合之前的学习笔记以及參考<Objective-C编程全解(第三版)>,对Objective-C知识点进行梳理总结.知识点一直在变,仅仅是作为參考,以苹果官方文档为准~ 十八.谓语的使用及 ...