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. jQuery的jsop,jsonp跨域请求

    https://www.cnblogs.com/chiangchou/p/jsonp.html

  2. PCB InCAM 获取 JOB STEP 实现外挂脚本调试功能实现

    PCB CAM自动化基于Incam 打造,在测试时经常遇到调试障碍,每次自行对功能测试时,生成了exe脚本后,再到Incam里面运行,发现问题,再回来修改代码,非常不爽, 参考Genesis调试运行模 ...

  3. Linux系统下vim常用快捷键及功能

    1. 什么是vim Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器,在vi的基础上改进和增加了很多特性. vim编辑器是Linux系统下标准的编辑器,作用相当于windows系统中的记 ...

  4. Linux基本命令 文件管理 上部

    第1章 Linux入门相关 目录基本知识 Linux一切从根开始 倒挂的树形结构 对路径与相对路径 绝对路径: 从根开始的路径 比如:/oldboy  /data 相对路径: 没有从根开始的路径 比如 ...

  5. TypeScript `unknown` 类型

    unknown 字面理解和 any 其实没差,任何类型都可赋值给它,但有一点, Anything is assignable to unknown, but unknown isn't assigna ...

  6. Python多线程、多进程

    1.from  multiprocessing import Process ;  from  threading import Thread 2.进程之间的数据传输 ,一般会使用到pipes, qu ...

  7. HDU 1847 博弈

    sg[0]=0; sg[i]=mex{sg[i-2^(j)]}  (i>=2^j) mex()为不在此集合的最小非负整数 #include <stdio.h> #include &l ...

  8. ACM_寻找第N小序列

    寻找第N小序列 Time Limit: 2000/1000ms (Java/Others) Problem Description: Now our hero finds the door to th ...

  9. mysql数据库存储的引擎和数据类型

    一.查看支持的存储引擎 SHOW ENGINES \G; 或者 SHOW VARIABLES LIKE 'have%'; 二.安装版mysql的默认引擎是InnoDB,免安装版默认引擎是MyISAM ...

  10. maven 纯注解一步一步搭建Spring Mvc项目(入门)

    初次接触spring MVC项目,通过一段时间的学习,本文介绍一种以纯注解的方法去配置spring MVC环境,让那些配置的.xml文件统统见鬼吧. 什么是Spring MVC Spring MVC属 ...