HDU 1856 More is better(基础并查集)
来源:互联网 发布:知乎 珠宝陈列师 编辑:程序博客网 时间:2024/05/09 15:41
最近一直在练习图论,写了不少并查集题目,大多数都是基础题,稍微总结一下
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)Total Submission(s): 15079 Accepted Submission(s): 5553
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.
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
41 23 45 61 641 23 45 67 8
Sample Output
42HintA 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<iostream>#include<cstdio>#include<cstring>using namespace std;#define N 10000005int num[N],f[N];void init(){ for(int i=0; i<N; i++) //懒省事,也可以找到输入数据种最大值 { f[i]=i; num[i]=1; }}int find(int x){ if(x!=f[x]) { f[x]=find(f[x]); return f[x]; }}int main(){ int n; while(cin>>n) { init(); int Max=1;//初始值1,开始乱设个0,一直错,没看清题 while(n--) { int x,y; scanf("%d%d",&x,&y); x=find(x); y=find(y); if(x!=y) { f[x]=y; num[y]+=num[x]; Max=max(num[y],Max); } } cout<<Max<<endl; }}
0 0
- HDU 1856 More is better(基础并查集)
- hdu 1856 More is better(并查集基础)
- HDU 1856 More is better 基础并查集★
- hdu 1856 More is better 基础并查集★
- hdu 1856 More is better 并查集(二)
- hdu 1856 More is better(并查集)
- hdu 1856 More is better (并查集)
- HDU 1856 More is better (并查集)
- HDU 1856 More is better (并查集)
- hdu 1856 More is better(并查集)
- hdu 1856 More is better(并查集)
- hdu 1856 More is better (并查集)
- HDU 1856 More is better(并查集)
- hdu 1856 More is better (并查集)
- 【HDU】-1856-More is better(并查集)
- HDU:1856 More is better(并查集+技巧)
- hdu 1856 More is better (并查集)
- HDU 1856 More is better(并查集)
- HDUOJ 2037 今年暑假不AC
- 于虚拟机Ubuntu系统arm-linux-gcc的安装
- leetcode:Rotate List
- 【蓝牙低功耗BLE】控制GPIO来点亮LED
- groovy : poi 导出 Excel
- HDU 1856 More is better(基础并查集)
- 讲一讲对Activity的了解【Android】
- Windows 7客户端安装与配置教学视频(笔记)
- 看了没吃过饭回来看见发的链接发的看见了客户的分开了
- jQuery中scrollTop获取匹配元素相对滚动条顶部的偏移(此方法对可见和隐藏元素均有效。)
- Wiki OI 1099 字串变换(双向BFS)
- 对接爱快递快递API接口
- 2014.10.04
- JSTL学习笔记