Hdoj 1856.More is better 题解
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
思路
裸并查集,只要找到所有分出来的集合当中元素最多的集合就行了,为此需要一个数组来记录个数,详见代码
代码
#include<bits/stdc++.h>
using namespace std;
int father[10000010];
int num[10000010];
int MaxValue;
void init(int n)
{
for(int i=1;i<=n;i++)
{
father[i]=i;
num[i] = 1;
}
}
int find(int x)
{
if(father[x]!=x) father[x] = find(father[x]);
return father[x];
}
void join(int a,int b)
{
int t1=find(a);
int t2=find(b);
if(t1!=t2)
{
father[t1]=t2;
num[t2] += num[t1];
MaxValue = max(num[t2],MaxValue);
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
{
cout << 1 << endl;
continue;
}
init(10000000);
MaxValue = -1;
for(int i=1;i<=n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(find(a) != find(b)) join(a,b);
}
cout << MaxValue << endl;
}
return 0;
}
Hdoj 1856.More is better 题解的更多相关文章
- 并查集(HDOJ 1856)
并查集 英文:Disjoint Set,即“不相交集合” 将编号分别为1…N的N个对象划分为不相交集合, 在每个集合中,选择其中某个元素代表所在集合. 常见两种操作: n 合并两个集合 ...
- HDOJ 1856
#include<cstdio> #include<cstdlib> typedef struct ufse *ufset; struct ufse { ]; ]; }UFS; ...
- HDOJ 1856 More is better
转自:wutianqi http://www.wutianqi.com/?p=1069 tag:并查集 #include <iostream> using namespace std; # ...
- hdoj 1856 More is better【求树的节点数】
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...
- Hdoj 1517.A Multiplication Game 题解
Problem Description Stan and Ollie play the game of multiplication by multiplying an integer p by on ...
- Hdoj 1392.Surround the Trees 题解
Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...
- Hdoj 1115.Lifting the Stone 题解
Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...
- Hdoj 2108.Shape of HDU 题解
Problem Description 话说上回讲到海东集团推选老总的事情,最终的结果是XHD以微弱优势当选,从此以后,"徐队"的称呼逐渐被"徐总"所取代,海东 ...
- Hdoj 1213.How Many Tables 题解
Problem Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. ...
随机推荐
- Effective java ---遵守普遍接受的命名规则
alibaba的java命名规范如下: . [强制]代码中的命名均不能以下划线或美元符号开始,也不能以下划线或美元符号结束. 反例: _name / __name / $Object / name_ ...
- CSS颜色代码 颜色值 颜色名字大全
颜色值 CSS 颜色使用组合了红绿蓝颜色值 (RGB) 的十六进制 (hex) 表示法进行定义.对光源进行设置的最低值可以是 0(十六进制 00).最高值是 255(十六进制 FF).从 0 到 25 ...
- javac与java版本不一致
项目测试时遇到该问题,因为loadRunner不支持jdk1.7,但运行java脚本时提示jdk版本是1.7,实际的JAVA_HOME设置为1.6. 运行javac -version与java -ve ...
- centos6.8安装JDK
1.检测当前系统安装jdk信息 rpm -qa | grep jdk 2.如果检查到有安装信息,则用sudo yum remove XXX(XXX代表上面查到的结果) 3.下载rpm包,安 ...
- qtp10 安装笔记
windows10系统安装QTP 10 1 QTP10 程序文件夹下,找到“setup”双击它运行安装程序-点击 否 继续安装 2 安装必要组件 3 下一步 选择安装程序目录-安装插件 直到完成安装 ...
- delphi 中出现dataset not in edit or insert mode的问题
self.ADOQuery2.Edit;self.ADOQuery2.First;while not self.ADOQuery2.Eof dobeginself.ADOQuery2.FieldByN ...
- github---无命令可视化界面操作
最近工作需要,研究了一下git,这个东西挺实用,给我的感觉并不是那么简单使用,我认为还可以再深入的研究一下,挺好玩的~ 说一下我的学习路线: 1.先看的廖老师的博客:https://www.liaox ...
- Memcached 分布式集群
首先解释一下我的标题,用到了 分布式 和 集群两个单词,为什么是集群?解决[相同业务]问题的服务器多个以上就称为集群.这里memcached就是做相同任务的(提供缓存服务)为什么是分布式?虽然针对的是 ...
- Maven依赖范围及传递
.Maven因为执行一系列编译.测试和部署运行等操作,在不同的操作下使用的classpath不同,依赖范围就是用来控制依赖与三种 classpath(编译classpath.测试classpath.运 ...
- jQuery 操作Cookie
一个轻量级的cookie 插件,可以读取.写入.删除 cookie. 下载地址:http://plugins.jquery.com/cookie/ (在实际中可以用这个保存cookie保存用户的习惯, ...