HDU 1856 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=1856
More is better
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others)
Total Submission(s): 29843 Accepted Submission(s): 10605
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
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.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
#define pii pair<int,int>
#define inf 0x3f3f3f3f
int f[],tot[];
int getf(int v){return f[v]==v?v:f[v]=getf(f[v]);}
map<int,int>M;
int main()
{
int N,i,j,k;
while(scanf("%d",&N)==){memset(tot,,sizeof(tot));
if(N==){puts("");continue;}
M.clear();
int p=,ans=,u,v;
for(i=;i<=;++i)f[i]=i;
for(i=;i<=N;++i)
{
scanf("%d%d",&u,&v);
int _u=M[u];
int _v=M[v];
if(!_u){M[u]=++p;_u=p;}
if(!_v){M[v]=++p;_v=p;}
int fu=getf(_u);
int fv=getf(_v);
if(fu!=fv){
f[fv]=fu;
}
}
for(i=;i<=p;++i) tot[getf(i)]++;
for(i=;i<=p;++i) ans=max(ans,tot[i]);
printf("%d\n",ans);
}
return ;
}
HDU 1856 并查集的更多相关文章
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- HDU 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
- HDU 1213(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 4496(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...
随机推荐
- IO 包中的其他类
打印流 PrintWriter 和 PrintWriter 直接操作输入流和文件 序列流 SequenceInputStream 对多个输入流进行合并 操作对象 ObjectInputStream 和 ...
- python数据类型三(字典)
一.字典的介绍 字典(dict)是python中唯一的一个映射类型,它是以{}括起来的键值对组成,在dict中key是唯一的,在保存的时候,根据key来计算出一个内存地址,然后将key-value保存 ...
- 什么是EJB
学习EJB可以加深对J2EE平台的认识. 百科定义EJB: 被称为java企业bean,服务器端组件,核心应用是部署分布式应用程序.用它部署的系统不限定平台.实际上ejb是一种产品,描述了应用组件要解 ...
- Tkprof工具详解一(转载)
在数据库生成的oracle trace文件中,可读性是比较差的,此时可使用tkprof工具来格式化trace文件,tkprof是一个命令行工具,作用就是把原始的跟踪trace文件作为输入,然后格式化一 ...
- 5 TensorFlow入门笔记之RNN实现手写数字识别
------------------------------------ 写在开头:此文参照莫烦python教程(墙裂推荐!!!) ---------------------------------- ...
- 吴超老师课程--HBASE的Java_API
public static void main(String[] args) throws IOException { String tableName="hbase_tb"; S ...
- 谷歌Project Fi服务(转)
问:谷歌推出的移动虚拟网络Project Fi到底是什么呀? 答:谷歌手里有很多张“牌”可以出,现在这家搜索巨头又将目标放在了无线产业.在美国,移动电信合约服务是AT&T, Verizon, ...
- JQuery Ajax调用WCF实例以及遇到的问题
1.遇到的最多的问题就是跨域问题,这个时间需要我们添加如下代码解决跨域的问题 第一步:在服务类加Attribute [AspNetCompatibilityRequirements(Requireme ...
- 应用服务器支持 HTTPS
当前业务系统中支持 HTTP 协议和 HTTPS 协议的 Web.config 文件并不相同.在默认情况下,不能同时支持 HTTPS 和 HTTP 协议. 生成部署包 若需支持 HTTPS 协议,请将 ...
- ibatis $与#的区别
在sql配置中比如in(#rewr#) 与in ($rewr$) 在Ibatis中我们使用SqlMap进行Sql查询时需要引用参数,在参数引用中遇到的符号#和$之间的区分为,#可以进行与编译,进行类型 ...