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. C语言之链接库

    链接库是windows的术语,但对于Linux来说,其概念是一样的.我们通常会把一些相似或相近功能的程序生成链接库,这样的好处是: 1)便于共享,开发软件时如需要相同功能时,不需要将大量重复的代码整合 ...

  2. LeetCode 637. Average of Levels in Binary Tree(层序遍历)

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  3. MIP启发式求解:局部搜索 (local search)

    *本文主要记录和分享学习到的知识,算不上原创. *参考文献见链接. 本文讲述的是求解MIP问题的启发式算法. 启发式算法的目的在于短时间内获得较优解. 个人认为局部搜索(local search)几乎 ...

  4. PAT Basic 1065

    1065 单身狗 “单身狗”是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ 50 000),是已知夫妻/伴侣 ...

  5. HDU 2829 斜率优化DP Lawrence

    题意:n个数之间放m个障碍,分隔成m+1段.对于每段两两数相乘再求和,然后把这m+1个值加起来,让这个值最小. 设: d(i, j)表示前i个数之间放j个炸弹能得到的最小值 sum(i)为前缀和,co ...

  6. mvn 发布

    mvn clean install deploy -Dadditionalparam=-Xdoclint:none

  7. Oracle排错总结

    一.Oracle常规恢复之不安全恢复 http://www.cnblogs.com/jyzhao/p/4723994.html#2.11

  8. Hive中文注释乱码解决方案

    本文来自网易云社区 作者:王潘安 快速解决方法 目前的hive客户端在执行desc tablexxx和show create table xxx命令的时候,字段的中文注释会出现乱码情况,如(????) ...

  9. redux学习总结

    redux学习总结 *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !imp ...

  10. ubuntu 转换图片格式的方法(sam2p, imagemagick)

    (1) 终端:sudo apt-get install sam2p sam2p [原图片名.格式] [目标图片名.格式] 即可在同一目录下生成目标图片格式 (2) 终端: sudo apt-get i ...