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 }
- 最长递增子序列
- dedecms批量删除文档关键词可以吗
- 【学术】在C ++中使用TensorFlow训练深度神经网络
- 一个canonical标签解决site不在首页的问题
- 由一道面试题来了解进程间的通信
- 【教程】简单教程:用Python解决简单的水果分类问题
- 通过html<map>标签给图片加链接
- Windows下安装Scikit-Learn
- 解决后台无法进入提示DedeCMS Error: (PHP 5.3 and above) Please set 'request_order' ini value
- 一文教你实现skip-gram模型,训练并可视化词向量
- 如何将文章列表用<li>分两列显示
- 面试题之走楼梯问题
- 最长公共子序列与最长公共子串
- nodejs 的序列化与反序列化
- 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 数组属性和方法
- Android studio 禁用AndroidX方式
- Android 实现把bitmap图片的某一部分的颜色改成其他颜色
- AndroidStudio构建项目提示错误信息“unable to find valid certification”的完美解决方案
- Android自定义View实现抖音飘动红心效果
- Android Studio 利用Splash制作APP启动界面的方法
- android surfaceView实现播放视频功能
- Android Studio中一套代码多渠道打包的实现方法
- Android自定义View实现圆环进度条
- Android启动页用户相关政策弹框的实现代码
- 10个好用的 HTML5 特性
- Android实现签名涂鸦手写板
- Android 开发使用Activity实现加载等待界面功能示例
- 详解Android使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou实现手指滑动效果
- Android开发自定义控件之折线图实现方法详解
- Android Studio实现长方体表面积计算器