HDU1865--More is better(统计并查集的秩(元素个数))
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
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.
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)
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
2
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.
#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(统计并查集的秩(元素个数))的更多相关文章
- More is better(hdu 1856 计算并查集集合中元素个数最多的集合)
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...
- BZOJ4668: 冷战 [并查集 按秩合并]
BZOJ4668: 冷战 题意: 给定 n 个点的图.动态的往图中加边,并且询问某两个点最早什 么时候联通,强制在线. 还可以这样乱搞 并查集按秩合并的好处: 深度不会超过\(O(\log n)\) ...
- 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 ...
- 【bzoj4668】冷战 并查集按秩合并+朴素LCA
题目描述 1946 年 3 月 5 日,英国前首相温斯顿·丘吉尔在美国富尔顿发表“铁幕演说”,正式拉开了冷战序幕. 美国和苏联同为世界上的“超级大国”,为了争夺世界霸权,两国及其盟国展开了数十年的斗争 ...
- Dash Speed【好题,分治,并查集按秩合并】
Dash Speed Online Judge:NOIP2016十联测,Claris#2 T3 Label:好题,分治,并查集按秩合并,LCA 题目描述 比特山是比特镇的飙车圣地.在比特山上一共有 n ...
- POJ 1611 The Suspects (并查集+数组记录子孙个数 )
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 24134 Accepted: 11787 De ...
- 【BZOJ-4668】冷战 并查集 + 按秩合并 + 乱搞
4668: 冷战 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 37 Solved: 24[Submit][Status][Discuss] Des ...
- BC68(HD5606) 并查集+求集合元素
tree Accepts: 143 Submissions: 807 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...
- BZOJ4025 二分图 分治 并查集 二分图 带权并查集按秩合并
原文链接http://www.cnblogs.com/zhouzhendong/p/8683831.html 题目传送门 - BZOJ4025 题意 有$n$个点,有$m$条边.有$T$个时间段.其中 ...
随机推荐
- P2P直连?经服务器中转?
当同一个系统的两个客户端A.B相互发送消息给对方时,如果它们之间存在P2P通道,那么消息传送的路径就有两种:直接经P2P通道传送.或者经服务器中转.如下图所示: 通常就一般应用而言,如果P2P通道能够 ...
- asp.net接收发送的xml字符串数据
最近研究为新的接口,发现发送的消息的格式为xml类型.之前接口接触的多是接收json类型的数据,可以直接通过Request["Key"]/Request.QueryString[& ...
- 与malloc有关的问题
nefu 1026 申请动态空间存放字符串,将其排序后输出 http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1026 #in ...
- amazeui 后台模板
<!doctype html> <html class="no-js"> <head> <meta charset="utf-8 ...
- chapter 12_2 保存无环的table
保存table有几种方法,选用哪种方法取决于对table的结构作出了哪些限制性的假设 第一个方法: function serialize(o) if type(o) == "number&q ...
- ural 1356. Something Easier(数论,哥德巴赫猜想)
1356. Something Easier Time limit: 1.0 secondMemory limit: 64 MB “How do physicists define prime num ...
- UIScrollView 加载多个view view还可以交换顺序(1)
#import "ViewController.h" #define kWidthOfScreen self.view.frame.size.width #define kHeig ...
- mysql、sqlserver数据库常见数据类型对应java中的的类型探究
由于本次测试表的结构不涉及到主键的自增长,所以mysql.sqlserver建表语句相同: CREATE TABLE testType ( id INT NOT NULL DEFAULT 0, gen ...
- HDU1577-WisKey的眼神
Problem Description WisKey的眼镜有500多度,所以眼神不大好,而且他有个习惯,就是走路喜欢看着地(不是为了拣钱哦^_^),所以大家下次碰见他的时候最好主动打下招呼,呵呵.但是 ...
- 【第一篇】学习 android 事件总线androidEventbus之sticky事件的传递
最近再看eventbus相关代码,首先从使用开始,后期再从源码角度分析eventbus.使用Demo后期公布到github上去. 使用的框架地址:https://github.com/bboyfeiy ...