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

  1. 4
  2. 1 2
  3. 3 4
  4. 5 6
  5. 1 6
  6. 4
  7. 1 2
  8. 3 4
  9. 5 6
  10. 7 8

Sample Output

  1. 4
  2. 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.

 
  题意:
    要找一些人完成一项工程。要求最后挑选出的人之间都是朋友关系,可以说直接的,也可以是间接地。问最多可以挑选出几个人(最少挑一个)。
    在基础的并查集上加个数组记录集合的数量。
 
  1. #include<cstdio>
  2. #include<algorithm>
  3. using namespace std;
  4. #define N 10000000
  5. int n,a,b,max0,max1,i,num[N],fa[N];
  6. int find(int a)
  7. {
  8. if(a == fa[a])
  9. {
  10. return a;
  11. }
  12. else
  13. {
  14. return fa[a]=find(fa[a]);
  15. }
  16. }
  17. void f1(int x,int y)
  18. {
  19. int nx,ny;
  20. nx=find(x);
  21. ny=find(y);
  22. if(nx != ny)
  23. {
  24. fa[nx]=ny;
  25. num[ny]+=num[nx]; //合并两个集合的数量
  26. }
  27. }
  28. int main()
  29. {
  30. while(scanf("%d",&n)!=EOF)
  31. {
  32. for(i = ; i <= 1e7 ; i++)
  33. {
  34. fa[i]=i;
  35. num[i]=; //刚开始集合只有本身
  36. }
  37. if(n == )
  38. {
  39. printf("1\n");
  40. continue;
  41. }
  42. max0=;
  43. for(i = ; i < n ; i++)
  44. {
  45. scanf("%d %d",&a,&b);
  46. max0=max(max0,max(a,b)); //找出关系中编号最大的人
  47. f1(a,b);
  48. }
  49. max1=;
  50. for(i = ; i <= max0 ; i++)
  51. {
  52. if(max1 < num[i]) //比较每个集合的大小
  53. {
  54. max1=num[i];
  55. }
  56. }
  57. printf("%d\n",max1);
  58. }
  59. }

杭电 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. More is better——并查集求最大集合(王道)

    Description Mr Wang wants some boys to help him with a project. Because the project is rather comple ...

  4. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 并查集求奇偶元环

    D. Dividing Kingdom II   Long time ago, there was a great kingdom and it was being ruled by The Grea ...

  5. C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

    C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. HDU 3018 Ant Trip (并查集求连通块数+欧拉回路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 题目大意:有n个点,m条边,人们希望走完所有的路,且每条道路只能走一遍.至少要将人们分成几组. ...

  7. 杭电 1213 How Many Tables (并查集求团体数)

    Description Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius ...

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

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

  9. 【并查集】 不相交集合 - 并查集 教程(文章作者:Slyar)

    最近写了一个多星期的并查集,一瞬间贴出这么多解题报告,我想关于并查集的应用先告一段落吧,先总结一下. 在网上看到一篇关于并查集比较好的教程(姑且允许我这么说吧),不转过来是在可惜.献给爱学习的你 文章 ...

随机推荐

  1. P2024 [NOI2001]食物链

    第一种说法是"1 X Y",表示 X 和 Y 是同类. 第二种说法是"2 X Y",表示 X 吃 Y . 根据这句话 可以看出来这是一个种类并查集 种类并查集 ...

  2. CSS3向外扩散的圆

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  3. nginx添加模块

    [root@VM_0_3_centos nginx]# ./sbin/nginx -V nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 ...

  4. nginx上游模块

    1 概念 The ngx_http_upstream_module is used to define groups of servers that can be referenced by the  ...

  5. Xcode7.1环境下上架iOS App到AppStore 流程 转

    来自:http://www.cnblogs.com/ChinaKingKong/p/4957682.html 前言部分 之前App要上架遇到些问题到网上搜上架教程发现都是一些老的版本的教程 ,目前iT ...

  6. COPY, RETAIN, ASSIGN , READONLY , READWRITE,STRONG,WEAK,NONATOMIC整理--转

    copy:建立一个索引计数为1的对象,然后释放旧对象 对NSString 对NSString 它指出,在赋值时使用传入值的一份拷贝.拷贝工作由copy方法执行,此属性只对那些实行了NSCopying协 ...

  7. fgetcsv()函数

    fgetcsv()函数.fgetcsv()函数可以读取指定文件的当前行,使用CSV格式解析出字段,并返回一个包含这些字段的数组.语法格式如下:array fgetcsv(resource $handl ...

  8. nodejs+multiparty 文件上传

    通过表单提交上传文件:     html代码 <form action="/uploadFile" method="post" enctype=" ...

  9. windows下常用的一些shell命令

    看的视频上都是linux系统的shell命令,和windows区别很多.所以整理了windows常用的一些shell命令. 注意:并不是每个都试验过,使用时还需自己验证下. 学system和os,su ...

  10. linux下自定义pid实现任意数据采集

    当你需要采集特殊的数据,而不满足于现有的你所知的数据模版时,自定义oid将是你必须而且非常好的解决方式. oid是snmp服务器为每条系统信息提供的唯一标识符,如果不能很好理解snmp服务的话,可以将 ...