hdu杭电1856 More is better【并查集】
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.
(A ≠ B, 1 ≤ A, B ≤ 10000000)
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
4
2HintA 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<stdio.h>
#define max 10000000
int per[max+100];
int num[max+100]; int find(int x)//找根节点
{
int r;
r=x;
while(r!=per[r])
{
r=per[r];
}
per[x]=r;
return r;
}
void join(int x ,int y)//连接两根节点
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
per[fx]=fy;
num[fy]+=num[fx];//当前根节点的元素
}
}
int main()
{
int a,b,m,n,i;
int t;
while(~scanf("%d",&t))
{
if(t==0)
{
printf("1\n");
continue;
}
for(i=1;i<=max;++i)//初始化
{
per[i]=i;
num[i]=1; //每一个节点仅仅有一个元素,自己本身
}
n=-1;//为了省时间,
while(t--)
{
scanf("%d%d",&a,&b);
if(a>n)
{
n=a;
}
if(b>n)
{
n=b;
}
join(a,b);
}
int MAX=0;
for(i=1;i<=n;++i)//在这用上
{
if(MAX<num[i]) MAX=num[i];
}
printf("%d\n",MAX);
}
return 0;
}
hdu杭电1856 More is better【并查集】的更多相关文章
- 杭电OJ——1198 Farm Irrigation (并查集)
畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...
- 【杭电OJ3938】【离线+并查集】
http://acm.hdu.edu.cn/showproblem.php?pid=3938 Portal Time Limit: 2000/1000 MS (Java/Others) Memo ...
- 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)
今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...
- 一个人的旅行 HDU杭电2066【dijkstra算法 || SPFA】
pid=2066">http://acm.hdu.edu.cn/showproblem.php? pid=2066 Problem Description 尽管草儿是个路痴(就是在杭电 ...
- 『ACM C++』HDU杭电OJ | 1418 - 抱歉 (拓扑学:多面体欧拉定理引申)
呕,大一下学期的第一周结束啦,一周过的挺快也挺多出乎意料的事情的~ 随之而来各种各样的任务也来了,嘛毕竟是大学嘛,有点上进心的人多多少少都会接到不少任务的,忙也正常啦~端正心态 开心面对就好啦~ 今天 ...
- HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...
- ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线
hdu 1811 Rank of Tetris Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & % ...
- HDU 1598 find the most comfortable road 并查集+贪心
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...
- HDU 1213 How Many Tables(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...
随机推荐
- LSTM 应用于股票市场
https://zhuanlan.zhihu.com/p/27112144 1.LSTM对于非平稳数据的预测效果没有平稳数据好 2.神经网络的过拟合:在训练神经网络过程中,“过拟合”是一项尽量要避免的 ...
- day01_02.php的开发环境准备
PHP开发环境的准备 此套课程推荐xampp,也就是Apache+Mysql+PHP 但是我自己的机器装的是wamp环境,稍微有一些不一样,但是不影响使用
- SCOI 2010 滑雪
题目描述 a180285非常喜欢滑雪.他来到一座雪山,这里分布着 MM 条供滑行的轨道和 NN 个轨道之间的交点(同时也是景点),而且每个景点都有一编号 ii ( 1 \le i \le N1≤i≤N ...
- verilog 实现中值滤波
图像信号在形成.传输和记录的过程中,由于成像系统.传输介质.工作环境和记录设备等的固有缺陷,不可避免地产生各种类型的噪声,降低了图像的质量,进而影响后续处理(如边缘检测.图像分割.特征提取.模式识别等 ...
- 【Luogu】P2515软件安装(树形DP)
题目链接 这么水的题我一遍没A,而且前两次提交都只有十分.气死我了.本来我的博客拒收水题来着. Tarjan缩点之后跑树形DP即可. #include<cstdio> #include&l ...
- NOJ——1665夜神的思考(YY+组合问题+分类讨论)
[1665] 夜神的思考 时间限制: 1000 ms 内存限制: 65535 K 问题描述 最近夜神对二进制很感兴趣,于是他每次看到一串只包含1和0的字符串的时候就会想,这串字符串有多少子串是含有k个 ...
- LibreOJ2095 - 「CQOI2015」选数
Portal Description 给出\(n,k,L,R(\leq10^9)\),求从\([L,R]\)中选出\(n\)个可相同有顺序的数使得其gcd为\(k\)的方案数. Solution 记\ ...
- spring中MessageSource的配置使用方法1
Spring定义了访问国际化信息的MessageSource接口,并提供了几个易用的实现类.首先来了解一下该接口的几个重要方法: String getMessage(String code, Ob ...
- webpack打包字体图标报错的解决办法
webpack打包字体图标需要两个加载器 url-loader 和 file-loader 另外 字体图标的引入方式 本来应该是 url("....") 这样的方式,但是w ...
- FOJ Problem 2257 Saya的小熊饼干
...