[HDU 1856] More is better
2015-06-04 17:26
162 查看
More is better
Time Limit: 5000/1000 MS (Java/Others)Memory Limit: 327680/102400 K (Java/Others)
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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)
[align=left]Output[/align]
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
[align=left]Sample Input[/align]
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
[align=left]Sample Output[/align]
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.
[align=left] [/align]
[align=left]【题解】并查集问题:求最大rank[/align]
[align=left]我加入了路径压缩和按秩合并。[/align]
[align=left]然后轻松AC了= =[/align]
[align=left]注意的是有一个特判,n==0时,输出1.[/align]
[align=left]注意不需要开始就全部初始化,那样复杂度为O(10000005*T) T为数据组数,那么肯定受不了。[/align]
#include<bits/stdc++.h> using namespace std; int pre[10000005],r[10000005],n,maxx; bool vis[10000005]; int find(int x) { int r=x; while(pre[r]!=r) r=pre[r]; int i=x,j; while(i!=r) { j=pre[i]; pre[i]=r; i=j; } return r; } void join(int a,int b) { int a1=find(a),b1=find(b); if (a1!=b1) { pre[a1]=b1; r[b1]+=r[a1]; if (r[b1]>maxx) maxx=r[b1]; } } int main() { while(scanf("%d",&n)!=EOF) { maxx=-1;memset(vis,0,sizeof(vis)); if(n==0) {printf("1\n"); continue;} for (int i=1;i<=n;++i) { int a,b; scanf("%d%d",&a,&b); if(!vis[a]) { vis[a]=1; pre[a]=a; r[a]=1; } if(!vis[b]) { vis[b]=1; pre[b]=b; r[b]=1; } join(a,b); } printf("%d\n",maxx); } return 0; }
View Code
相关文章推荐
- RDIFramework.NETV2.9版本 Web新增至14套皮肤风格+三套界面组合(共42套皮肤组合)
- Git在协同开发中的使用经验分享
- live555源代码分析
- 持续部署,并不简单!
- Gradle学习系列之十——自定义Plugin(本系列完)
- IN 查询时出现ORA-01795:列表中的最大表达式数为1000解决方法
- Android笔记三十四.Service综合实例二
- 我读经典(9):读《完美软件开发:方法与逻辑》有感
- 课程建议
- <数据结构学习与实验指导>3-3银行业务队列模拟/3-4一元多项式的乘法与加法运算
- taskAffinity属性
- Gradle学习系列之九——自定义Task类型
- Android版CSDN发现的一些问题
- Download Ubuntu realeased history versions
- C++派生类的构成
- 类日历插件——技术储备
- sql执行疑问
- 简单内存泄漏检测方法 解决 Detected memory leaks! 问题
- Gradle学习系列之八——构建多个Project
- nodejs exports和module.exports区别