Problem Description
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.
 
Input
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)
 
Output
The output in one line contains exactly one integer equals to the maximum number of boys Mr Wang may keep.
 
Sample Input
4
1 2
3 4
5 6
1 6
4
1 2
3 4
5 6
7 8
 
Sample Output
4
2
Hint
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<stdio.h>
#define max 10000000
int per[max+100];
int num[max+100]; int find(int x)//找根节点
{
int r;
r=x;
while(r!=per[r])
{
r=per[r];
}
per[x]=r;
return r;
}
void join(int x ,int y)//连接两根节点
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
{
per[fx]=fy;
num[fy]+=num[fx];//当前根节点的元素
}
}
int main()
{
int a,b,m,n,i;
int t;
while(~scanf("%d",&t))
{
if(t==0)
{
printf("1\n");
continue;
}
for(i=1;i<=max;++i)//初始化
{
per[i]=i;
num[i]=1; //每一个节点仅仅有一个元素,自己本身
}
n=-1;//为了省时间,
while(t--)
{
scanf("%d%d",&a,&b);
if(a>n)
{
n=a;
}
if(b>n)
{
n=b;
}
join(a,b);
}
int MAX=0;
for(i=1;i<=n;++i)//在这用上
{
if(MAX<num[i]) MAX=num[i];
}
printf("%d\n",MAX);
}
return 0;
}

hdu杭电1856 More is better【并查集】的更多相关文章

  1. 杭电OJ——1198 Farm Irrigation (并查集)

    畅通工程 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出了每条道路直接连通的城镇.省政府"畅通工程"的目标是使全省任何两个城镇间都可 ...

  2. 【杭电OJ3938】【离线+并查集】

    http://acm.hdu.edu.cn/showproblem.php?pid=3938 Portal Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  3. 『ACM C++』HDU杭电OJ | 1415 - Jugs (灌水定理引申)

    今天总算开学了,当了班长就是麻烦,明明自己没买书却要带着一波人去领书,那能怎么办呢,只能说我善人心肠哈哈哈,不过我脑子里突然浮起一个念头,大二还要不要继续当这个班委呢,既然已经体验过就可以适当放下了吧 ...

  4. 一个人的旅行 HDU杭电2066【dijkstra算法 || SPFA】

    pid=2066">http://acm.hdu.edu.cn/showproblem.php? pid=2066 Problem Description 尽管草儿是个路痴(就是在杭电 ...

  5. 『ACM C++』HDU杭电OJ | 1418 - 抱歉 (拓扑学:多面体欧拉定理引申)

    呕,大一下学期的第一周结束啦,一周过的挺快也挺多出乎意料的事情的~ 随之而来各种各样的任务也来了,嘛毕竟是大学嘛,有点上进心的人多多少少都会接到不少任务的,忙也正常啦~端正心态 开心面对就好啦~ 今天 ...

  6. HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...

  7. ACM: hdu 1811 Rank of Tetris - 拓扑排序-并查集-离线

    hdu 1811 Rank of Tetris Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  8. HDU 1598 find the most comfortable road 并查集+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000 ...

  9. HDU 1213 How Many Tables(模板——并查集)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday ...

随机推荐

  1. Coursera无法观看课程解决方案

    Coursera无法观看课程解决方案 最近Cousera一直表现不佳,课程视频无法观看.小编结合网上找到的信息,操作一番便解决了问题,视频也可以正常观看了. 首先是win+s找到记事本,并用管理员身份 ...

  2. POJ 2051 Argus

    Argus Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8782   Accepted: 3985 Description ...

  3. isinstance 和 type 的区别

    class A: pass class B(A): pass isinstance(A(), A) # returns True type(A()) == A # returns True isins ...

  4. Bootstrap-table custome-ajax用法

    <div id="toolbar"> <div class="form-inline" role="form"> & ...

  5. 数据表自动生成java代码

    MyBatis生成代码需要用到mybatis-generator-core-1.3.2.jar.数据库连接驱动包和一个xml文件,xml文件一般命令为:generator.xml. Xml内容格式如下 ...

  6. iOS学习笔记39-ReactiveCocoa入门

    FRP,全称为Functional Reactive Programming,是一种响应变化的编程范式,最近几年比较火,大概的理解就像这样: 当a的值或者b的值发生变化时,c的值会自动响应a的值或b的 ...

  7. BZOJ1064 [Noi2008]假面舞会 【dfs】

    题目 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一 个自己喜欢的面具.每个面具都有一个编号,主办方会把此编号告诉拿 ...

  8. SVN改地址eclipse怎么同步

    步骤有3 : 首先保证:Windows-> preservences->SVN的接口Client为 1.8以上的 1 )   打开eclipse中SVN资源库 在Eclipse中选择Win ...

  9. 【前端学习笔记】2015-09-02 附~~~~~ajax简单请求和获得响应结果

    首先得创建一个请求XMLHttpRequest对象,var  xmlhttp=window.XMLHttpRequest?new XMLHttpRequest() : new ActiveXObjec ...

  10. UGUI 点击穿透问题

    unity上 用 做游戏欢迎界面上通用的ui,然后导到游戏里面直接用,但发现游戏里面是用ngui的,点击ugui 的ui 会穿透过去 ngui会响应,原本模型的点击处理也会响应 我用的 unity 版 ...