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. PCB 录屏工具Screen2Exe GifCam ScreenToGif

    我们完成的软件作品后,需要向客户或领导演示软件功能介绍,这里力推3款录屏工具 一.Screen2Exe工具,录制exe视频文件 下载地址  http://pcbren.cn/ShareFiles/Sc ...

  2. E20170626-hm

    authenticate   vt. 认证,证明是真实的.可靠的或有效的; 鉴定,使生效; author   n. 作者; 著作家; 创造者; 发起人;

  3. [Swift通天遁地]五、高级扩展-(4)快速生成Invert、Mix、Tint、Shade颜色及调整饱和度阶

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  4. NHibernate学习(零)-本次学习遇到的错误汇总

    问题一: "System.TypeInitializationException"类型的未经处理的异常在 KimismeDemo.exe 中发生 其他信息: "NHibe ...

  5. wait、notify、notifyAll实现线程间通信

    在Java中,可以通过配合调用Object对象的wait()方法和notify()方法或notifyAll()方法来实现线程间的通信.在线程中调用wait()方法,将阻塞等待其他线程的通知(其他线程调 ...

  6. photoshop制作古风画

    效果图: 素材结构: 前期准备: 素材准备,我的素材包括:印章.花束.二次元妹纸,背景图片. 软件准备,用的软件是 photoshop CS6. 把二次元妹纸拖入photoshop,把她抠出来,Ctr ...

  7. React Native常用组件在Android和IOS上的不同

    React Native常用组件在Android和IOS上的不同 一.Text组件在两个平台上的不同表现 1.1 height与fontSize 1.1.1只指定font,不指定height 在这种情 ...

  8. [Android]有关外部链接唤醒App需要注意的坑

    移动互联网发展到今天,一个移动app需要和各种各样的外部链接关联,它不再仅仅从手机的桌面启动,更多的将会从其他的应用.浏览器链接.短信.二维码或者微信分享等渠道启动,这里涉及到的是各种各样的营销渠道和 ...

  9. 一个有趣的 ”Validation of viewstate MAC failed” 错误的发现和解决

    在ASP.NET里面,View State使用较为广泛.它作为一个隐藏字段,可以帮助服务端”记住“客户端的改变,这样客户端 收到服务器对PostBack的响应后,仍然可以展现在PostBack之前设定 ...

  10. HDU_1063_Exponentiation_大数

    Exponentiation Time Limit: 2000/500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...