题意:

Mr Wang wants some boys to help him with a project. Because the project is rather complex, the more boys come, the better it will be. Of course there are certain requirements.

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.

The 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)

思路:

简单并查集

代码:

int n;
struct node{
int x,y;
}
Pair[100005]; int cn;
int a[200005];
int ct[200005];
int fa[200005]; int findFa(int x){
return fa[x]==x?fa[x]:fa[x]=findFa(fa[x]);
} void Union(int x,int y){
int fx=findFa(x);
int fy=findFa(y);
if(fx!=fy){
fa[fx]=fy;
}
} int main(){
map<int,int> mp; while(scanf("%d",&n)!=EOF){
mp.clear();
cn=0;
rep(i,1,n){
scanf("%d%d",&Pair[i].x,&Pair[i].y);
if(mp[Pair[i].x]==0){
mp[Pair[i].x]=1;
a[++cn]=Pair[i].x;
}
if(mp[Pair[i].y]==0){
mp[Pair[i].y]=1;
a[++cn]=Pair[i].y;
}
}
sort(a+1,a+1+cn);
rep(i,1,cn) fa[i]=i;
rep(i,1,n){
int px=lower_bound(a+1,a+1+cn,Pair[i].x)-a;
int py=lower_bound(a+1,a+1+cn,Pair[i].y)-a;
Union(px,py);
}
mem(ct,0);
int ans=1;
rep(i,1,cn){
int t=findFa(i);
ct[t]++;
ans=max( ans,ct[t] );
}
printf("%d\n",ans);
} return 0;
}

hdu 1856 More is better(并查集)的更多相关文章

  1. HDU 1856 More is better (并查集)

    题意: 给你两个数代表这两个人是朋友,朋友的朋友还是朋友~~,问这些人组成的集合里面人最多的是多少... 思路: 属于并查集了,我用的是带路径压缩的,一个集合里面所有元素(除了根节点)的父节点都是根节 ...

  2. HDU 1856 More is better(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...

  3. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...

  4. hdu 1257 小希的迷宫 并查集

    小希的迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...

  5. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

  6. <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)

     本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...

  7. HDU 4496 D-City(逆向并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=4496 题意: 给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数. 思路: 离线处理删边,从后 ...

  8. HDU 3407.Zjnu Stadium 加权并查集

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

  10. HDU 4641 K-string 后缀自动机 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=4641 https://blog.csdn.net/asdfgh0308/article/details/4096 ...

随机推荐

  1. DebugView端游日志查看工具

    端游日志工具 端游开发的同学可以通过DebugView - Windows Sysinternals | Microsoft Docs来查看游戏打印的log,它允许你监控本地系统上的debug pri ...

  2. 剑指offer计划20( 搜索与回溯算法中等)---java

    1.1.题目1 剑指 Offer 07. 重建二叉树 1.2.解法 注释解法. 1.3.代码 class Solution { int[] preorder; HashMap<Integer, ...

  3. 关于buildroot移植的思考

    buildroot是一个成熟的SDK框架,基于它有了openwrt. 曾经有一个项目,需要将原有的OpenWrt SDK改造,并且将软件框架重新定义.尝试精简原来的OpenWrt,并且删除所有的软件包 ...

  4. Nginx系列(5)- nginx: [emerg] bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)

    启动Windows版本的Nginx时候,cmd报错,报错信息为[emerg] 4276#4280: bind() to 0.0.0.0:80 failed(10013: An attempt was ...

  5. Elasticsearch2.4.6版本 在linux 命令行 对数据的增删改操作

    一._cluster系列:查询设置集群状态 1.设置集群状态 curl -XGET 10.68.120.167:9204/_cluster/health?pretty=true pretty=true ...

  6. MySQL 服务无法启动。 服务没有报告任何错误。 请键入 NET HELPMSG 3534 以获得更多的帮助。

    第一步 有一部分人成功了在安装根目录创建data文件夹 第二步 有一部分人成功了D:\Program Files\mysql\bin>mysqld initialize 第三步 这步之后我安装成 ...

  7. LR进行内外网附件上传并发——实践心得

    刚开始接触LR的时候,做了一次内外网附件上传的并发测试,比较简单,但当时理解有些欠缺.以下为当时的实践心得: 1.分内外网测试的意义: 内网测试主要看负载压力情况等,外网测试主要考虑网络带宽.网络延时 ...

  8. pyqt5 GUI教程

    from PyQt5 import QtCore, QtGui, QtWidgets import sys import qtawesome class MainUi(QtWidgets.QMainW ...

  9. AT4144-[ARC098D]Donation【Kruskal重构树,dp】

    正题 题目链接:https://www.luogu.com.cn/problem/AT4144 题目大意 \(n\)个点\(m\)条边的一张无向联通图,每个点有两个值\(a_i,b_i\).表示经过该 ...

  10. c++ class里面成员和分配内存问题

    慢慢开始学c++啦,记录学习的大体过程 class中神奇的内存(sizeof) 1.内存补齐 便于管理类(生成的对象)的内存,类总内存总是为最大成员字节大小的倍数,不足的会进行内存补齐 类的整体内存就 ...