More is better

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 25168    Accepted Submission(s): 9045

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
 
这道题目需要统计并查集的秩
只需要加一个数组记录根节点的秩, 然后在合并时, 顺带着将秩也合并即可
 #include <cstdio>
#include <cstdlib>
#include <climits> const int MAX = ;
int pre[MAX],rank[MAX],maxx; void init(){
int i;
for(i=;i<MAX;++i){
pre[i] = i;
rank[i] = ;
}
} int root(int x){
if(x!=pre[x]){
pre[x] = root(pre[x]);
}
return pre[x];
} void merge(int x,int y){
int fx = root(x);
int fy = root(y);
if(fx!=fy){
pre[fx] = fy;
rank[fy] += rank[fx];//更改对应元素的秩
if(rank[fy]>maxx)maxx = rank[fy];//更新最大值
}
} int main(){
//freopen("in.txt","r",stdin);
int i,n,x,y;
while(scanf("%d",&n)!=EOF){
if(n==){
printf("1\n");
continue;
}
init();
maxx = INT_MIN;
for(i=;i<n;++i){
scanf("%d %d",&x,&y);
merge(x,y);
}
printf("%d\n",maxx);
}
return ;
}
 

HDU1865--More is better(统计并查集的秩(元素个数))的更多相关文章

  1. More is better(hdu 1856 计算并查集集合中元素个数最多的集合)

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  2. BZOJ4668: 冷战 [并查集 按秩合并]

    BZOJ4668: 冷战 题意: 给定 n 个点的图.动态的往图中加边,并且询问某两个点最早什 么时候联通,强制在线. 还可以这样乱搞 并查集按秩合并的好处: 深度不会超过\(O(\log n)\) ...

  3. POJ 1703 Find them, Catch them【种类/带权并查集+判断两元素是否在同一集合/不同集合/无法确定+类似食物链】

      The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the ...

  4. 【bzoj4668】冷战 并查集按秩合并+朴素LCA

    题目描述 1946 年 3 月 5 日,英国前首相温斯顿·丘吉尔在美国富尔顿发表“铁幕演说”,正式拉开了冷战序幕. 美国和苏联同为世界上的“超级大国”,为了争夺世界霸权,两国及其盟国展开了数十年的斗争 ...

  5. Dash Speed【好题,分治,并查集按秩合并】

    Dash Speed Online Judge:NOIP2016十联测,Claris#2 T3 Label:好题,分治,并查集按秩合并,LCA 题目描述 比特山是比特镇的飙车圣地.在比特山上一共有 n ...

  6. POJ 1611 The Suspects (并查集+数组记录子孙个数 )

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24134   Accepted: 11787 De ...

  7. 【BZOJ-4668】冷战 并查集 + 按秩合并 + 乱搞

    4668: 冷战 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 37  Solved: 24[Submit][Status][Discuss] Des ...

  8. BC68(HD5606) 并查集+求集合元素

    tree  Accepts: 143  Submissions: 807  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65 ...

  9. BZOJ4025 二分图 分治 并查集 二分图 带权并查集按秩合并

    原文链接http://www.cnblogs.com/zhouzhendong/p/8683831.html 题目传送门 - BZOJ4025 题意 有$n$个点,有$m$条边.有$T$个时间段.其中 ...

随机推荐

  1. P2P直连?经服务器中转?

    当同一个系统的两个客户端A.B相互发送消息给对方时,如果它们之间存在P2P通道,那么消息传送的路径就有两种:直接经P2P通道传送.或者经服务器中转.如下图所示: 通常就一般应用而言,如果P2P通道能够 ...

  2. asp.net接收发送的xml字符串数据

    最近研究为新的接口,发现发送的消息的格式为xml类型.之前接口接触的多是接收json类型的数据,可以直接通过Request["Key"]/Request.QueryString[& ...

  3. 与malloc有关的问题

    nefu 1026 申请动态空间存放字符串,将其排序后输出 http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1026 #in ...

  4. amazeui 后台模板

    <!doctype html> <html class="no-js"> <head> <meta charset="utf-8 ...

  5. chapter 12_2 保存无环的table

    保存table有几种方法,选用哪种方法取决于对table的结构作出了哪些限制性的假设 第一个方法: function serialize(o) if type(o) == "number&q ...

  6. ural 1356. Something Easier(数论,哥德巴赫猜想)

    1356. Something Easier Time limit: 1.0 secondMemory limit: 64 MB “How do physicists define prime num ...

  7. UIScrollView 加载多个view view还可以交换顺序(1)

    #import "ViewController.h" #define kWidthOfScreen self.view.frame.size.width #define kHeig ...

  8. mysql、sqlserver数据库常见数据类型对应java中的的类型探究

    由于本次测试表的结构不涉及到主键的自增长,所以mysql.sqlserver建表语句相同: CREATE TABLE testType ( id INT NOT NULL DEFAULT 0, gen ...

  9. HDU1577-WisKey的眼神

    Problem Description WisKey的眼镜有500多度,所以眼神不大好,而且他有个习惯,就是走路喜欢看着地(不是为了拣钱哦^_^),所以大家下次碰见他的时候最好主动打下招呼,呵呵.但是 ...

  10. 【第一篇】学习 android 事件总线androidEventbus之sticky事件的传递

    最近再看eventbus相关代码,首先从使用开始,后期再从源码角度分析eventbus.使用Demo后期公布到github上去. 使用的框架地址:https://github.com/bboyfeiy ...