hdu 1856(hash+启发式并查集)
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 22283 Accepted Submission(s): 8106
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.
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)
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
2
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = ;
const int M = ;
int Hash[M];
int cnt[N];
int father[N],dep[N]; int _find(int x){
if(x==father[x]) return x;
return _find(father[x]);
}
int Union(int a,int b){
int x = _find(a);
int y = _find(b);
if(x==y) return ;
if(dep[x]==dep[y]){
father[x] = y;
dep[y]++;
}else if(dep[x]<dep[y]){
father[x] = y;
}else father[y]=x;
return ;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
if(n==) {
printf("1\n");
continue;
}
memset(Hash,-,sizeof(Hash));
memset(cnt,,sizeof(cnt));
for(int i=;i<=*n;i++){
father[i] = i;
dep[i]=;
}
int k=;
for(int i=;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
if(Hash[a]==-){
Hash[a]=++k;
}
if(Hash[b]==-){
Hash[b]=++k;
}
Union(Hash[a],Hash[b]);
}
for(int i=;i<=*n;i++){
cnt[_find(i)]++;
}
int Max = -;
for(int i=;i<=*n;i++){
Max = max(Max,cnt[i]);
}
printf("%d\n",Max);
}
}
hdu 1856(hash+启发式并查集)的更多相关文章
- Hdu 1856(离散化+并查集)More is better
题意:一些人遵循朋友的朋友也是朋友原则,现在找出最大的朋友圈, 因为人的编号比较大,但是输入的数据最多是10w行,所以可得出最多也就20w人,所以可以进行一下离散化处理,这样数据就会毫无压力 //// ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
- HDU 2473 Junk-Mail Filter 并查集,虚拟删除操作
http://acm.hdu.edu.cn/showproblem.php?pid=2473 给定两种操作 第一种是合并X Y 第二种是把X分离出来,就是从原来的集合中分离出来,其它的关系不变. 关键 ...
- <hdu - 1232> 畅通工程 并查集问题 (注意中的细节)
本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 结题思路:因为题目是汉语的,那我就不解释题意了,要求的是最少建设的道路,我们可以用并查集来做这 ...
- HDU 5441 Travel(并查集+统计节点个数)
http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...
- HDU 4313 Matrix(并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=4313 题意: 给出一棵树,每条边都有权值,其中有几个点是特殊点,现在破坏边还使得这几个特殊点互相不可达,需要使得 ...
- hdu 1558 (线段相交+并查集) Segment set
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐 ...
- HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...
随机推荐
- 质数,$\varphi$和$\mu$线性筛
typedef long long ll; bool check[N]; int mu[N],pri[N],tot; ll phi[N]; void init(int lim){ check[]=,p ...
- python几个复习例子
1.实现1-100的所有的和,程序代码如下: sum = 0 for i in xrange(1,101): sum +=i print (sum) 程序运行结果: 2.实现1-500所有奇数的和,程 ...
- 【Java集合源码剖析】Java集合框架
Java集合工具包位于Java.util包下,包含了很多常用的数据结构,如数组.链表.栈.队列.集合.哈希表等.学习Java集合框架下大致可以分为如下五个部分:List列表.Set集合.Map映射.迭 ...
- sourceInsight *** more bytes are required
现象:用sourceinsight修改的文件无法保存,提示 No enough space to save "XXX", xxx more bytes are required. ...
- Kotlin的数据类:节省很多行代码(KAD 10)
作者:Antonio Leiva 时间:Jan 25, 2017 原文链接:https://antonioleiva.com/data-classes-kotlin/ 在前面的文章中,我们已经见到了类 ...
- C# MemoryCache 类[转载]
原网址:http://www.cmono.net/post/read/156 MemoryCache 类是.Net .0推出的类库,主要是为了方便在Winform和Wpf中构建缓存框架的 Object ...
- 解压大文件提示C盘空间不够的问题
问题说明 今天在服务器解压一个之前上传的数据,大概有180GB,虽然当前盘还有984GB的富余. 但是当我选择解压到当前文件夹时,解压到半路还是提醒C盘的空间不足. 原理 压缩文件解压会在C盘创建一个 ...
- OpenFlow-Enaling innvation in Campus Networks
OpenFlow-Enaling innvation in Campus Networks 出现问题 背景 Networks have become part of the critical infr ...
- rsync同步数据
1. rsync 命令格式rsync [OPTION]... SRC DESTrsync [OPTION]... SRC [USER@]HOST:DESTrsync [OPTION]... [USER ...
- RabbitMQ-Java客户端API指南-上
RabbitMQ-Java客户端API指南-上 客户端API严格按照AMQP 0-9-1协议规范进行建模,并提供了易于使用的附加抽象. RabbitMQ Java客户端使用com.rabbitmq.c ...