More is better

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)

Total Submission(s): 18427 Accepted Submission(s): 6779

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
Recommend
lcy | We have carefully selected several similar problems for you:

pid=1325" target="_blank">
1325
1102 1301 1829

pid=1811" target="_blank">
1811



#include<stdio.h>
#include<string.h>
#include<algorithm>
int maxn=-1;
using namespace std;
int root[10000001];
int res[10000001];
int find(int i){
if(root[i]==i) return i;
return root[i]=find(root[i]);
}
void unio(int x,int y){
int p=find(x),q=find(y);
if(p<=q) root[q]=p,res[p]+=res[q],maxn=max(maxn,res[p]); // 在合并的时候顺便查找最大值。降低时间开销。否则超时!。! else root[p]=q,res[q]+=res[p],maxn=max(maxn,res[q]);
return;
}
int main(){
int n;
while(~scanf("%d",&n)){
if(n==0){
printf("1\n");
continue;
}
memset(res,0,sizeof(res));
int i,x,y,a,b;
maxn=-1;
for(i=1;i<=10000000;++i) root[i]=i,res[i]=1;
for(i=1;i<=n;++i){
scanf("%d%d",&a,&b);
x=find(a);y=find(b);
if(x!=y){
unio(x,y);
}
}
printf("%d\n",maxn);
}
return 0;
}

hdoj-1856-More is better【并查集】的更多相关文章

  1. hdoj 2473 Junk-Mail Filter【并查集节点的删除】

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. HDU 1856 More is better (并查集)

    题意: 给你两个数代表这两个人是朋友,朋友的朋友还是朋友~~,问这些人组成的集合里面人最多的是多少... 思路: 属于并查集了,我用的是带路径压缩的,一个集合里面所有元素(除了根节点)的父节点都是根节 ...

  3. HDU 1856 More is better(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...

  4. hdoj 4786 Fibonacci Tree【并查集+最小生成树(kruskal算法)】

    Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. hdoj 3478 Catch(二分图判定+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3478 思路分析:该问题需要求是否存在某一个时刻,thief可能存在图中没一个点:将该问题转换为图论问题 ...

  7. hdoj 1878 欧拉回路(无向图欧拉回路+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1878 思路分析:该问题给定一个无向图,要求判断该无向图是否存在欧拉回路:无向图判断存在欧拉回路的两个必 ...

  8. HDU 1213(并查集)

    How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. 并查集(HDOJ 1856)

    并查集   英文:Disjoint Set,即“不相交集合” 将编号分别为1…N的N个对象划分为不相交集合, 在每个集合中,选择其中某个元素代表所在集合. 常见两种操作: n       合并两个集合 ...

  10. 简单并查集 -- HDU 1232 UVALA 3644 HDU 1856

    并查集模板: #include<iostream> using namespace std; ],x,y; ]; //初始化 x 集合 void init(int n) { ; i< ...

随机推荐

  1. 如何用纯 CSS 创作一个雷达扫描动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/VdbGvr 可交互视频 ...

  2. Python把类当做字典来访问

    定义一个类将它实例化,我们可以通过obj.属性来访问类的属性,如果想获取类的所有实例变量,我们可以使用obj.__dict__来访问,如下: class A: def __init__(self): ...

  3. appIcon

    原文地址:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconM ...

  4. Lex与Yacc学习(七)之环境配置另一种方式

    必备工具 flex-2.5.4a-1.exe   和  bison-2.4.1-setup.exe   以及 cygwin2.738 的安装文件,下载地址 http://download.csdn.n ...

  5. POJ:2753-Seek the Name, Seek the Fame

    Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Description The little cat is s ...

  6. PAT Basic 1051

    1051 复数乘法 复数可以写成 (A+Bi) 的常规形式,其中 A 是实部,B 是虚部,i 是虚数单位,满足 i​2​​=−1:也可以写成极坐标下的指数形式 (R×e​(Pi)​​),其中 R 是复 ...

  7. React中css的使用

    网页的布局.颜色.形状等UI展示方式主要是由Css进行设置,在ReactJs中也是一样.ReactJs中的Css结构方式与传统的Web网页类似,但依然存在一些差异.ReactJs中Css文件本身的编写 ...

  8. HDU 3516 DP 四边形不等式优化 Tree Construction

    设d(i, j)为连通第i个点到第j个点的树的最小长度,则有状态转移方程: d(i, j) = min{ d(i, k) + d(k + 1, j) + p[k].y - p[j].y + p[k+1 ...

  9. Eclipse截取android报错log

    Eclipse截取android报错log: 1.前提条件:已安装eclipse 2. LogCat界面设置: Logcat是Android 编程中一个命令行工具,可以用于得到程序的 log 信息,可 ...

  10. sql server 2008启动时:已成功与服务器建立连接,但是在登录过程中发生错误。(provider:命名管道提供程序,error:0-管道的另一端上无任何进程。)(Microsoft SQL Server,错误:233) 然后再连接:错误:18456

    问题:sql server 2008启动时:已成功与服务器建立连接,但是在登录过程中发生错误.(provider:命名管道提供程序,error:0-管道的另一端上无任何进程.)(Microsoft S ...