More is better

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

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
 
题意:王老师要找一些男生帮助他完成一项工程。要求最后挑选出的男生之间都是朋友关系,可以说直接的,也可以是间接地。问最多可以挑选出几个男生(最少挑一个)。

         这是一个有关并查集的题目。只不过不是求有几个集合,而是求每个集合中元素的个数,进而求出个数的最大值。和求集合个数的方法差不多,只需要在合并两个集合时处理一下,让这两个集合的元素个数也合并一下就行了。接下来只需要找出最大值即可。要注意的一个地方就是:当n=0时,要输出1。
 
 #include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define MAX 10000010 using namespace std; int pre[MAX],s[MAX]; int find(int x)
{
int i,t,r;
r=x;
while(r!=pre[r])
r=pre[r];
while(x!=r)
{
i=pre[x];
pre[x]=r;
x=i;
}
return r;
}
int main()
{
int i,n;
while(scanf("%d",&n)!=EOF)
{
int i,j,k,a,b,t=,pa,pb,max=;
memset(pre,,sizeof(pre));
memset(s,,sizeof(s));
if(n==)
{
printf("1\n");
continue;
}
for(i=;i<MAX;i++)
{
pre[i]=i;
s[i]=; //开始时数量都为1,根节点为自己
}
for(i=;i<n;i++)
{
scanf("%d %d",&a,&b);
if(a>t) t=a;
if(b>t) t=b;
pa=find(a);pb=find(b);
//printf("%d %d\n",pa,pb);
if(pa!=pb)
{
pre[pb]=pa;
s[pa]+=s[pb]; //合并集合中元素个数
}
}
for(i=;i<=t;i++)
{
if(s[i]>max)
max=s[i];
}
printf("%d\n",max);
}
return ;
}

hdu_1856_More is better_201403091720的更多相关文章

随机推荐

  1. $(document).ready 与 window.onload的区别?

    $(document).ready  = function(){}; DOM树加载完成时执行,此时文件不一定都已加载完成. window.onload = function(){}; DOM树加载完成 ...

  2. error: undefined reference to 'property_set (转载)

    转自:http://blog.csdn.net/u011589606/article/details/23474241 in the cpp file, please include #include ...

  3. [Apple开发者帐户帮助]七、注册设备(3)禁用或启用设备

    您可以禁用和启用设备,但不能从开发者帐户中删除它.您可以禁用不再用于开发或测试的设备.但是,这样做会使包含设备的配置文件无效,并且不会增加当年设备的总数. 所需角色:帐户持有人或管理员. 在“ 证书” ...

  4. JS/JQuery操作DOM元素笔记

    原因 自己目前在搭建一个.NET Core的框架,正在构建权限这块的东西,今天设置权限界面,需要使用JavaScript操作DOM元素,记录一下. 页面大概是酱紫的(我使用的AdminLTE和LayU ...

  5. vscode----vue中HTML代码tab键自动补全

    1.在vscode中插件下载并重新加载HTML Snippets 2.settings.json中配置files.associations对象. 找到setting.json文件:文件 --> ...

  6. MySQL的or/in/union与索引优化 | 架构师之路

    假设订单业务表结构为: order(oid, date, uid, status, money, time, …) 其中: oid,订单ID,主键 date,下单日期,有普通索引,管理后台经常按照da ...

  7. linq 分组

    var data = from r in listRecords group r by new { r.CampaignId, r.CityId, r.Gift_DistributorId, r.Pr ...

  8. python 求一个文件中每个字符出现的次数

    import pprint import collections filename = input('Input filename') with open(filename) as info: cou ...

  9. GridView动态计算高度

    // 动态加载GridView 高度 public static void setListViewHeightBasedOnChildren(MyGridView myGridView) { List ...

  10. CentOS7配置VSFTP服务器

    [1] 安装VSFTP [root@localhost ~]# yum -y install vsftpd [2] 配置vsftpd.conf文件 [root@localhost ~]# vi /et ...