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 ...
随机推荐
- es6总结(二)
1.es6三种声明方式: a.var 全局声明 b.let 局部变量声明 c.const 常量声明 2.变量的解构赋值 a.数组的解构赋值 等号左边与右边形式统一 let [a,[b,c] ...
- firstChild与firstElementChild
相同点: 都是获取父元素下的第一个节点对象 不同点: firstChild: IE6.7.8 第一个元素节点; 非IE6.7.8:返回第一个元素节点或文本节点 firstElementChild: I ...
- SQL 学习——简序以及学习路线
1.最近发现自己除去简单的SQL语句好像其他的并不怎么懂哎,虽然暂时是android用不到太复杂点的语句,想来总不能一直这样把, 顺带还是看看Sql. 2.画个图规划下自己的学习路线
- 动态数组C++实现
回顾大二的数据结构知识.从数组开始.实现了一个可自动扩充容量的泛型数组. 头文件:Array.h #ifndef Array_hpp #define Array_hpp template <cl ...
- Navicat for Mysql 关于1130错误,无法正常方法解决的解决办法。
本人因为失误操作,不小心将mysql 玩崩了.导致一直报1130错误,无法进入. 看了很多网上的帖子,但是那些办法都行不通.最后通过好友的指点,解决了这个问题. 1.停止MySQL服务,执行net ...
- UNP学习笔记1——基本TCP套接字编程
1 套接字地址结构 大多数套接字函数都需要一个指向套接字地址结构的指针作为参数.每个协议族都定义了自己的套接字结构.这些套接字的结构以sockaddr_开头,以每个协议族唯一的后缀名结尾. 1.1 I ...
- linux进程的有效用户ID
进程的有效用户ID用于文件访问时的权限检查.通常,有效用户ID等于实际用户ID(也就是你登录是的用户ID),有效组ID等于实际组ID. 我们知道每个文件针对不同的user有不同的读.写.执行权限.当执 ...
- LeetCode Golang 6. Z 字形变换
6. Z 字形变换 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...
- python3 继承与组合
什么叫继承? 所谓继承,就是class_A里面的功能从class_B中直接获取,从而节约了代码且使用方便. 什么叫组合? 除了继承,还有一种我们可以实现目的的方式,那就是组合,同样可以节约代码.只不过 ...
- [剑指offer] 7. 斐波那契数列 (递归 时间复杂度)
简介: 杨辉三角每条斜线上的数之和就构成斐波那契数列. 思路: 参考文章:https://mp.weixin.qq.com/s?src=11×tamp=1551321876& ...