【题目链接】http://acm.hdu.edu.cn/showproblem.php?pid=1856

【解题思路】给的数据有点大,干脆少开点数组,直接上set存储有朋友的孩子的编号,同时根据编号初始化对应的在father数组中的下标,同时也更新集合,后面开一个数组存储同属于于一个集合的数有多少个(在以根为数组的下标中存储),然后遍历一次找到最大的数

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <set>
  4. #include <algorithm>
  5. #define SIZE 10000001
  6. #define MAXN 100002
  7. using namespace std;
  8. int father[SIZE];
  9. int num[SIZE];
  10.  
  11. set<int>exist;
  12.  
  13. int find(int f)
  14. {
  15. return father[f] = f == father[f] ? f : find(father[f]);
  16. }
  17.  
  18. int main()
  19. {
  20. #ifndef ONLINE_JUDGE
  21. freopen("input.txt", "r", stdin);
  22. #endif
  23. int n;
  24. while(scanf("%d", &n) != EOF)
  25. {
  26. if(!n)
  27. {
  28. printf("1\n");
  29. continue;
  30. }
  31. exist.clear();
  32. memset(num, , sizeof(num));
  33. for(int i=; i<n; ++i)
  34. {
  35. int u, v;
  36. scanf("%d%d", &u, &v);
  37. if(exist.find(u) == exist.end())
  38. {
  39. father[u] = u;
  40. exist.insert(u);
  41. num[u] = ;
  42. }
  43. if(exist.find(v) == exist.end())
  44. {
  45. exist.insert(v);
  46. father[v] = v;
  47. num[v] = ;
  48. }
  49. u = find(u);
  50. v = find(v);
  51. father[u] = v;
  52. }
  53. for(set<int>::const_iterator iter = exist.begin(); iter != exist.end(); ++iter)
  54. {
  55. num[find(*iter)]++;
  56. }
  57. int curmax = ;
  58. for(set<int>::const_iterator iter = exist.begin(); iter != exist.end(); ++iter)
  59. {
  60. if(curmax < num[*iter]) curmax = num[*iter];
  61. }
  62. printf("%d\n", curmax);
  63. }
  64. return ;
  65. }

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

  1. hdu 5458 Stability(树链剖分+并查集)

    Stability Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 65535/102400 K (Java/Others)Total ...

  2. [HDU 3712] Fiolki (带边权并查集+启发式合并)

    [HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...

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

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

  4. ACM学习历程—HDU 1272 小希的迷宫(并查集)

    Description 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就 ...

  5. hdu 5652 India and China Origins 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...

  6. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  7. HDU 5458 Stability (树链剖分+并查集+set)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 给你n个点,m条边,q个操作,操作1是删边,操作2是问u到v之间的割边有多少条. 这题要倒着做才 ...

  8. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  9. hdu 5652 India and China Origins 并查集+逆序

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...

  10. HDU 5809 Ants(KD树+并查集)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5809 [题目大意] 给出一些蚂蚁和他们的巢穴,一开始他们会在自己的巢穴(以二维坐标形式给出),之后 ...

随机推荐

  1. CSS 将一个页面平均分成四个部分(div)

    在项目中遇到需求,数据监控页面需要同时显示4个板块内容,如下图: CSS 如何将一个页面平均分成四个部分(div)呢? <!DOCTYPE html> <html lang=&quo ...

  2. 多线程读者写者问题--用QT实现

    先把代码贴上来,有时间再整理吧..因为工作中用Qt,所以用Qt实现的....刚上班,,忙! 三种方法,读者优先,写者优先和公平竞争.. 读者优先 #include <QCoreApplicati ...

  3. Idea 编写 Spark 示例代码并打包成Jar

    说明:本人是在Linux下搭建的单机Spark环境,也是在Linux下使用Idea14.02进行代码编辑 1. 打开IDEA,在欢迎界面从右下角的Configure -> Plugins进入,安 ...

  4. Spring @RequestAttribute

    @RequestAttribute注解用法 @RequestAttribute用在方法入参上,作用:从request中取对应的值,至于request中是怎么存在该属性的,方式多种多样,拦截器中预存.M ...

  5. 自动化运维(1)之二进制部署MySQL5.7

    二进制部署MySQL5.7 这个文档用于基础解释,后面通过ansible的自动化对MySQL单实例进行安装部署. 1.解压文件 # tar zxvf mysql-5.7.22-linux-glibc2 ...

  6. [转]在Node.js中使用RabbitMQ系列一 Hello world

    本文转自:https://www.cnblogs.com/cpselvis/p/6288330.html 在前一篇文章中可伸缩架构简短系列中提到过关于异步的问题.当时推荐使用RabbitMQ来做任务队 ...

  7. csharp: Setting the value of properties reflection

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. 二维码编码与解码类库ThoughtWorks.QRCode

    官方地址:https://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library 有源代码和示例程序 支持二维码编码(生成)和解码( ...

  9. Windows安装diango框架<一>

    下一篇:使用Django创建网站项目<二> python工具安装 python下载:https://www.python.org/downloads/windows/(我的版本3.7.0) ...

  10. MyBatis学习笔记(三) Configuration类

    一.初探Configuration类 我们先来看一下MyBatis的XML配置文件的结构,(摘自mybatis.org) 下面这个是Configuration类的部分变量 一点不一样是不是??? 其实 ...