HDUOJ----More is better(并查集)
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 10473 Accepted Submission(s): 3877
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
HDU 2007 Programming Contest - Final
Recommend
lcy
http://acm.hdu.edu.cn/showproblem.php?pid=1856
并查集可以解。。。。求的是集合里面元素的个数:即求出最大的秩就行了!
代码:
1 #include<iostream>
2 #include<cstdio>
3 #define maxn 100001
4 using namespace std;
5 int father[maxn+5],rank[maxn+5];
6 void inite(int n)
7 {
8 for(int i=1;i<=n;i++)
9 {
10 father[i]=i;
11 rank[i]=1;
12 }
13 }
14
15 int findset(int x)
16 {
17 if(x!=father[x])
18 {
19 father[x]=findset(father[x]);
20 }
21 return father[x];
22 }
23
24 void unset(int x,int y)
25 {
26 x=findset(x);
27 y=findset(y);
28 if(x==y) return ;
29 if(rank[x]>rank[y])
30 {
31 father[y]=father[x];
32 rank[x]+=rank[y];
33 }
34 else
35 {
36 father[x]=father[y];
37 rank[y]+=rank[x];
38 }
39 }
40
41 int main()
42 {
43 int n,a,b,i,max;
44 while(scanf("%d",&n)!=EOF)
45 {
46 inite(maxn);
47 for(i=0;i<n;i++)
48 {
49 scanf("%d %d",&a,&b);
50 unset(a,b);
51 }
52 for(i=1;i<=maxn;i++)
53 {
54 if(i==1||max<rank[i])
55 max=rank[i];
56 }
57 printf("%dn",max);
58 }
59 return 0;
60 }
- 使用Python抓取欧洲足球联赛数据
- python爬取百度新闻:分析共享单车火爆背后有哪些规则?
- Python爬虫(urllib2+bs4)数据采集:分析找出百度贴吧谁是水贴王
- 学界 | OpenAI 发布稀疏计算内核,更宽更深的网络,一样的计算开销
- 【手把手教你做项目】自然语言处理:单词抽取/统计
- Kaggle赛题解析:逻辑回归预测模型实现
- R语言 使用BP神经网络进行银行客户信用评估
- 使用R语言挖掘QQ群聊天记录
- 解析滴滴算法大赛---GBDT进行数据预测
- 数据迁移中的数据库检查和建议(r2笔记71天)
- 决策树案例:基于python的商品购买能力预测系统
- 数据迁移前的准备和系统检查 (r2笔记70天)
- 数据处理的统计学习(scikit-learn教程)
- 机器学习实战,使用朴素贝叶斯来做情感分析
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 如何基于jackson动态序列化指定字段
- c/c++补完计划(五): 平衡二叉树和二叉搜索树
- 【原创】Java并发编程系列30 | ThreadLocal
- c/c++补完计划(六): 语法糖lambda
- 从别人的代码中学习golang系列--03
- Flink写出数据到HBase的Sink实现
- 「MoreThanJava」Day 4:面向对象基础
- RTSP协议视频平台EasyNVR接入EasyNVS通道不显示任何数据怎么处理?
- RTSP协议视频平台EasyNVR接入视频综合管理平台EasyNVS可以自由修改配置吗?
- 使用Azure人脸API对图片进行人脸识别
- 视频综合管理平台EasyNVS显示的rtsp和rtmp穿透地址错误如何解决?
- 开发RTSP协议视频平台EasyNVR采用ffmpeg集成fdk-aac报no member named “encoderDelay”
- 微信小程序开发实战(17):用画布画一张笑脸
- PHP中Session ID的实现原理分析和实例解析
- 【好文推荐】黑莓OS手册是如何详细阐述底层的进程和线程模型的?