HDU 1856 More is better【并查集】
解题思路:将给出的男孩的关系合并后,另用一个数组a记录以find(i)为根节点的元素的个数,最后找出数组a的最大值
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) Total Submission(s): 15861 Accepted Submission(s): 5843
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.
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
2
#include<stdio.h>
#include<string.h>
int pre[10000010],a[10000010];
int find( int root)
{
if(root!=pre[root])
pre[root]=find(pre[root]);
return pre[root];
}
void unionroot( int root1,int root2)
{
int x,y;
x=find(root1);
y=find(root2);
if(x!=y)
pre[x]=y;
} int main()
{
int n;
int root1,root2,x,y,k,i,max;
while(scanf("%d",&n)!=EOF)
{
max=-100000;
for(i=1;i<=10000010;i++)
a[i]=0; for(i=1;i<=10000010;i++)
pre[i]=i;
while(n--)
{
scanf("%d %d",&root1,&root2);
x=find(root1);
y=find(root2);
unionroot(x,y);
} for(i=1;i<=10000010;i++)
{
k=find(i);
a[k]++;//记录以find(i)为根节点的包含有多少 个元素
}
for(i=1;i<=10000010;i++)
{
if(a[i]>max)
max=a[i];
}
printf("%d\n",max);
}
}
HDU 1856 More is better【并查集】的更多相关文章
- HDU 1856 More is better (并查集)
题意: 给你两个数代表这两个人是朋友,朋友的朋友还是朋友~~,问这些人组成的集合里面人最多的是多少... 思路: 属于并查集了,我用的是带路径压缩的,一个集合里面所有元素(除了根节点)的父节点都是根节 ...
- HDU 1856 More is better(并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...
- HDU HDU1558 Segment set(并查集+判断线段相交)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...
- hdu 1257 小希的迷宫 并查集
小希的迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
- <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)
本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...
- HDU 4496 D-City(逆向并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=4496 题意: 给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数. 思路: 离线处理删边,从后 ...
- HDU 3407.Zjnu Stadium 加权并查集
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
- HDU 4641 K-string 后缀自动机 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=4641 https://blog.csdn.net/asdfgh0308/article/details/4096 ...
随机推荐
- C++之易混淆知识点五
一.解析类继承中父类与子类之间成员的访问可见度: 外部可见度:指的是被子类继承的父类成员在子类的外部访问控制度,有protected.public.private. 内部可见度:指的是被子类继承的父类 ...
- 对ListView的Item子控件监听并跳转页面
public class MyAdapteforOwner extends BaseAdapter{ List<OwnerDevice>datas; private Context con ...
- 互联网时代: 从Uber的供需匹配看开发需求
每次看电影中的有钱人都有专属司机接送,只要坐在车里,就有人帮忙开车门.提行李及关车门.感觉是非现实的遥远画面,现在却有机会可以在日常生活中成真! 2009年Travis Kalanick及Garret ...
- 记一次redis-cluster的切换
# redis-cli -h 10.5.8.18 -c -p 8001 cluster nodes|grep master 6d2f817064a10631648f24f450a37237b3d53f ...
- RunLoop主要处理以下6类事件
1.Observer事件,runloop中状态变化时进行通知.(微信卡顿监控就是利用这个事件通知来记录下最近一次main runloop活动时间,在另一个check线程中用定时器检测当前时间距离最后一 ...
- 第九章 Python之面向对象
面向对象编程 面向对象编程是一种程序设计思想,它把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数 面向过程的程序设计把计算机程序视为一系列命令的集合,即一组函数的顺序执行.为了简化程序设计 ...
- luogu p1004
P1004 题意 类似一个比较小的方格(N<=9),有的点是0,有的点有数, A->B的路径经过的点加上该点代表的数,求两次A->B的最大解(最优解) 一个令人恼的问题是两条路径如果 ...
- $attr和$listeners is readonly
https://www.jb51.net/article/132371.htm 出现这个问题的原因,主要是因为在使用的时候出现了A组件调用B组件,B组件再调用了C组件.而直接使用了A组件修改C组件的数 ...
- C语言使用memcpy函数实现两个数间任意位置的复制操作
c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中. 用法:void *memcpy(void *dest ...
- 加密中加salt的意思
所谓加Salt,就是加点“佐料”.当用户首次提供密码时(通常是注册时),由系统自动往这个密码里加一些“Salt值”,这个值是由系统随机生成的,并且只有系统知道.然后再散列.而当用户登录时,系统为用户提 ...