More is better-多多益善
题目描述:
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 roo
3 4
5 6
1 6
4
1 2m 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.
输入:
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)
输出:
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
样例输入:
4
1 2
3 4
5 6
7 8
样例输出:
4
2
思路:在图中所有的连通分量中找出包含顶点最多的个数。继续使用并查集解决!
#include <iostream>
using namespace std; const int MAX = ;
int tree[MAX];
int counts[MAX]; int getRoot(int x)
{
if (tree[x] == -)
return x;
else
{
int tmp = getRoot(tree[x]);
tree[x] = tmp;
return tmp;
}
} int main()
{
int n;
int town1, town2;
int result;
while (cin >> n && n != )
{
for (int i = ; i < MAX; i++)
{
tree[i] = -;
counts[i] = ;
} result = ;
for (int i = ; i <= n; i++)
{
cin >> town1 >> town2;
town1 = getRoot(town1);
town2 = getRoot(town2);
if (town1 != town2)
{
tree[town1] = town2;
counts[town2] += counts[town1];
} } for (int i = ; i <= MAX; i++)
{
if (result < counts[i])
result = counts[i];
} cout << result << endl;
}
return ;
}
More is better-多多益善的更多相关文章
- 又一个opengl教程,多多益善
http://ogldev.atspace.co.uk/index.html http://wiki.jikexueyuan.com/project/modern-opengl-tutorial/tu ...
- Web性能优化:图片优化
程序员都是懒孩子,想直接看自动优化的点:传送门 我自己的Blog:http://cabbit.me/web-image-optimization/ HTTP Archieve有个统计,图片内容已经占到 ...
- 程序员装B指南
一.准备工作 "工欲善其事必先利其器." 1.电脑不一定要配置高,但是双屏是必须的,越大越好,能一个横屏一个竖屏更好.一个用来查资料,一个用来写代码.总之要显得信息量很大,效率很高 ...
- ubuntu入门
Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音.了解发音是有意义的,您不是第一个为此困惑的人,当然,也不会是最后一个:) 大多数的美国人读 ubun ...
- html5 canvas简易版捕鱼达人游戏源码
插件描述:html5利用canvas写的一个js版本的捕鱼,有积分统计,鱼可以全方位移动,炮会跟着鼠标移动,第一次打开需要鼠标移出背景图,再移入的时候就可以控制炮的转动,因为是用的mouseover触 ...
- HTML5移动端图片左右切换动画
插件描述:HTML5移动端图片左右切换动画 小海今天要给大家分享一款很不错的图片左右切换焦点图动画,并且支持移动端触摸滑动.功能上,这款HTML5图片播放器支持鼠标滑动.手机端触摸滑动以及自动播放.外 ...
- C# 正则表达式
引用自:http://www.cnblogs.com/stg609/archive/2009/06/03/1492709.html 摘要:正则表达式(Regular Expressions),相信做软 ...
- Alamofire 的使用
最近,AFNetworking 的作者Mattt Thompson提交了一个新的类似于 AFNetworking 的网络 基础库,并且是专门使用最新的 Swift 语言来编写的,名为:Alamofir ...
- 【详解】ERP、APS与MES系统是什么?
ERP是什么?MES是什么?APS又是什么?无论他们有什么功能,对企业有什么意义,不过都是计算机在读写一些数据而已.实际上这一切的本质不过是数据在硬盘和内存中快速的读和写. ERP是--,APS是-- ...
- 探索javascript----浅析js模块化
引言: 鸭子类型: 面向对象的编程思想里,有一个有趣的概念,叫鸭子类型:“一只鸟走起来像鸭子.游起泳来像鸭子.叫起来也像鸭子,那它就可以被当做鸭子.也就是说,它不关注对象的类型,而是关注对象具有的行为 ...
随机推荐
- 自定义相机下使用clippingNode注意事项
调用完clippingNode->setCameraMask(myCameraMask)后,还需要clipNode->getStencil()->setCameraMask(myCa ...
- [hihoCoder] 骨牌覆盖问题·一
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 骨牌,一种古老的玩具.今天我们要研究的是骨牌的覆盖问题:我们有一个2xN的长条形棋盘,然后用1x2的骨牌去覆盖整个棋盘.对 ...
- jquery定时器的简单代码
当收到消息的时候能够及时的刷新,显示收到消息的条数,下面与大分享下使用简单的代码实现jquery定时器 简单的代码实现jquery定时器. 今天,项目遇到一个消息的模块,在导航条最上面.想实现,当收到 ...
- 【Android】17.0 第17章 服务绑定—本章示例主界面
分类:C#.Android.VS2015: 创建日期:2016-03-03 一.简介 通过服务绑定(Bound Services),可以轻松实现后台服务与界面(UI)的交互. 二.本章示例主界面 1. ...
- 每日英语:How to Be a Better Conversationalist
Jason Swett still cringes when he remembers the party in Atlanta 10 years ago, where, drink in hand, ...
- Linux命令(24) :sort
转载地址:点击 linux 命令详解 sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分钟搞定sort,现在开始! 1 sort的工作原理 sort将文件的每一行作为一个单位,相互比 ...
- SpringCloud | FeignClient和Ribbon重试机制区别与联系
在spring cloud体系项目中,引入的重试机制保证了高可用的同时,也会带来一些其它的问题,如幂等操作或一些没必要的重试. 今天就来分别分析一下 FeignClient 和 Ribbon 重试机制 ...
- rm: cannot remove `dir': Device or resource busy解决办法
使用df查看系统发现: [ops@bs038 cm-5.4.0]$ df -hFilesystem Size Used Avail Use% Mounted on/dev/sda3 1.1T 200G ...
- 在开发中写一些tool来提升自己的效率
比如说,我在调试一个web-project的时候,因为eclipse默认编译好的.class文件放在/build/classes/中,但是我希望随时把这些class文件放到/WEB-ROOT/WEB- ...
- vim复制粘贴常用命令
在Windows下我们习惯的操作,复制单个字符,复制单行多行,删除单行多行,在linux的vim中操作如下: G(shift+g+g):跳到文档尾 g+g:跳转到文档首 home键:光标移动到行首 e ...