题意:

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. 1.24学习总结——HTML常见标签

    HTML 标签简写及全称 下表列出了 HTML 标签简写及全称: 标签 英文全称 中文说明 a Anchor 锚 abbr Abbreviation 缩写词 acronym Acronym 取首字母的 ...

  2. POJ题目 1003Hangover(叠放纸牌)

    POJ 1003 叠放纸牌 描述 您可以将多张纸牌悬在桌子上多远?如果您有一张卡,则可以创建一个最大长度为卡长的一半.(我们假设这些卡片必须垂直于桌子.)使用两张卡片,您可以使最上面的卡片悬垂在底部的 ...

  3. 【PHP数据结构】其它排序:简单选择、桶排序

    这是我们算法正式文章系列的最后一篇文章了,关于排序的知识我们学习了很多,包括常见的冒泡和快排,也学习过了不太常见的简单插入和希尔排序.既然今天这是最后一篇文章,也是排序相关的最后一篇,那我们就来轻松一 ...

  4. Java面向对象系列(12)- Static关键字讲解

    场景一:静态变量 package oop.demo07; public class Student { private static int age;//静态的变量 一般多线程用的比较多 privat ...

  5. strategy策略模式个人理解

    首先了解策略模式的主要作用:能够把算法进行封装和动态传递: 可能听上去很抽象,我们引入一个方便理解的案例来解释: 给定一个数组 int[] array = {32,12,42,26,-23,0,-2, ...

  6. Cookie实现是否第一次登陆/显示上次登陆时间

    Cookie实现是否第一次登陆/显示上次登陆时间 最近刚好看到Cookie这方面知识,对Servlet部分知识已经生疏,重新翻出已经遗弃角落的<JavaWeb开发实战经典>,重新温习了Co ...

  7. 极简SpringBoot指南-Chapter04-基于SpringBoot的书籍管理Web服务

    仓库地址 w4ngzhen/springboot-simple-guide: This is a project that guides SpringBoot users to get started ...

  8. Java泛型中的细节

    Java泛型中的细节 如果没有泛型 学习Java,必不可少的一个过程就是需要掌握泛型.泛型起源于JDK1.5,为什么我们要使用泛型呢?泛型可以使编译器知道一个对象的限定类型是什么,这样编译器就可以在一 ...

  9. 使用率激增250%,这份报告再将 Serverless 推向幕前

    ​ 作者 | 望宸 来源 | Serverless 公众号 相比去年,国外 Serverless 的适用群体在迅速扩大,函数执行时长不断增加,使用方式也越加成熟,开发者工具也更加开放.本文是对 Dat ...

  10. Apache ShardingSphere 在京东白条场景的落地之旅

    京东白条使用 Apache ShardingSphere 解决了千亿数据存储和扩容的问题,为大促活动奠定了基础. 2014 年初,"京东白条"作为业内互联网信用支付产品,数据量爆发 ...