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$个时间段.其中 ...
随机推荐
- HDU 1264 Counting Squares(模拟)
题目链接 Problem Description Your input is a series of rectangles, one per line. Each rectangle is speci ...
- 对于querystring取值时候发生+号变空格的问题
今天遇到这个问题,在网上找了几个答案,解决了问题,很高兴,所以贴出来给大家分享一下: URL如下 http://127.0.0.1/test/test.aspx?sql= and id='300+' ...
- CreateCompatibleBitmap 需要注意的问题
不要使用CreateCompatibleDC得到的内存DC作为其参数,应使用真实DC,否则图片不能显示
- 第二次冲刺spring会议(第三次会议)
[例会时间]2014/5/6 21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳奇
- qq昵称由fly改为思诺
刚才,把高三申请的qq号,昵称fly 改为 思诺,英文名称为saynoer,say+no+er, 意思你懂的.
- Thrift项目Server端开发流程
Thrift项目Server端开发流程 首先,先了解工程中所有包的功能(见下图) 该图为用户中心项目的目录结构,以下依次介绍. 1. src/main/java com.framework:该 ...
- mysql 表连接
1.子查询是指在另一个查询语句中的SELECT子句. 例句: SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2); 其中,SELECT ...
- hdu_3062_Party(2-SAT)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3062/ 题意:2-SAT的裸题 题解:直接上模版 #include<cstdio> #in ...
- LeetCode OJ 66. Plus One
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- nefu 72 N!
Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one line, ...