The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 48327   Accepted: 23122

Description

Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. To minimize transmission to others, the best strategy is to separate the suspects from others. 
In the Not-Spreading-Your-Sickness University (NSYSU), there are many student groups. Students in the same group intercommunicate with each other frequently, and a student may join several groups. To prevent the possible transmissions of SARS, the NSYSU collects the member lists of all student groups, and makes the following rule in their standard operation procedure (SOP). 
Once a member in a group is a suspect, all members in the group are suspects. 
However, they find that it is not easy to identify all the suspects when a student is recognized as a suspect. Your job is to write a program which finds all the suspects.

Input

The input file contains several cases. Each test case begins with two integers n and m in a line, where n is the number of students, and m is the number of groups. You may assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and n−1, and initially student 0 is recognized as a suspect in all the cases. This line is followed by m member lists of the groups, one line per group. Each line begins with an integer k by itself representing the number of members in the group. Following the number of members, there are k integers representing the students in this group. All the integers in a line are separated by at least one space. 
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.

Output

For each case, output the number of suspects in one line.

Sample Input

  1. 100 4
  2. 2 1 2
  3. 5 10 13 11 12 14
  4. 2 0 1
  5. 2 99 2
  6. 200 2
  7. 1 5
  8. 5 1 2 3 4 5
  9. 1 0
  10. 0 0

Sample Output

  1. 4
  2. 1
  3. 1

题意:有n个点和m个点集,每个点集有k个点,每个点集中的个点可以相连,求与0相连的点的个数(包括0)。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <math.h>
  6. #include <limits.h>
  7. #include <map>
  8. #include <stack>
  9. #include <queue>
  10. #include <vector>
  11. #define ll long long
  12. #define INF 0x3f3f3f3f
  13. const int maxn=1e5+10;
  14. int a[maxn];
  15. int sum[maxn];
  16. int ans;
  17. int n,m;
  18. int k,x,y;
  19. int find(int x)
  20. {
  21. if(x!=a[x])
  22. {
  23. return find(a[x]);
  24. }
  25. return a[x];
  26. }
  27. void join(int x,int y)
  28. {
  29. int dx=find(x);
  30. int dy=find(y);
  31. //对dx(或dy)所在的点集进行个数统计
  32. if(dx!=dy)
  33. {
  34. a[dy]=dx;
  35. sum[dx]+=sum[dy];
  36. }
  37. }
  38. int main(int argc, char const *argv[])
  39. {
  40. while(std::cin>>n>>m&&n||m)
  41. {
  42. memset(a,0,sizeof(a));
  43. memset(sum,0,sizeof(sum));
  44. ans=1;
  45. for(int i=0;i<n;i++)
  46. {
  47. sum[i]=1;
  48. a[i]=i;
  49. }
  50. while(m--)
  51. {
  52. std::cin>>k;
  53. std::cin>>x;
  54. k--;
  55. while(k--)
  56. {
  57. std::cin>>y;
  58. join(x,y);
  59. }
  60. }
  61. //输出0所在点集的个数
  62. std::cout<<sum[find(0)]<<std::endl;
  63. }
  64. return 0;
  65. }

POJ 1611:The Suspects(并查集)的更多相关文章

  1. poj 1611 The Suspects(并查集输出集合个数)

    Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...

  2. poj 1611 The Suspects 并查集变形题目

    The Suspects   Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 20596   Accepted: 9998 D ...

  3. POJ 1611 The Suspects (并查集+数组记录子孙个数 )

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 24134   Accepted: 11787 De ...

  4. POJ 1611 The Suspects (并查集求数量)

    Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, wa ...

  5. POJ 1611 The Suspects 并查集 Union Find

    本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...

  6. poj 1611 The Suspects 并查集

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 30522   Accepted: 14836 De ...

  7. [ACM] POJ 1611 The Suspects (并查集,输出第i个人所在集合的总人数)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21586   Accepted: 10456 De ...

  8. 并查集 (poj 1611 The Suspects)

    原题链接:http://poj.org/problem?id=1611 简单记录下并查集的模板 #include <cstdio> #include <iostream> #i ...

  9. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

  10. poj 1611:The Suspects(并查集,经典题)

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 21472   Accepted: 10393 De ...

随机推荐

  1. Android ContentResolver

    在Android 应用程序之间数据共享—-ContentResolver中,已经说明了Android是如何实现应用程序之间数据共享的,并详细解析了如何获取其他应用 程序共享的数据.ContentPro ...

  2. 梅尔频率倒谱系数(MFCC) 学习笔记

    最近学习音乐自动标注的过程中,看到了有关使用MFCC提取音频特征的内容,特地在网上找到资料,学习了一下相关内容.此笔记大部分内容摘自博文 http://blog.csdn.net/zouxy09/ar ...

  3. 2017-2018-2 20165207实验二《Java面向对象程序设计》实验报告

    2017-2018-2 20165207实验二<Java面向对象程序设计>实验报告 课程:Java程序设计 班级:1652 姓名:李天林 学号:20165207 实验日期:2018年4月1 ...

  4. SQLite 自定义函数,聚合,排序规则

    SQLite 自定义函数,聚合,排序规则 1.使用自定义函数, 聚合以及排序规则的基本方法是使用回调函数.这些注册的函数的生命周期只存在于应用程序中, 并不存储在数据库文件中, 因此需要在每个连接建立 ...

  5. VC/MFC 编程技巧大总结

    1 toolbar默认位图左上角那个点的颜色是透明色,不喜欢的话可以自己改. 2 VC++中 WM_QUERYENDSESSION WM_ENDSESSION 为系统关机消息. 3 Java学习书推荐 ...

  6. Tomcat 发布项目 conf/Catalina/localhost 配置 及数据源配置

    本文介绍通过在tomcat的conf/Catalina/localhost目录下添加配置文件,来发布项目.因为这样对 tomcat 的入侵性最小,只需要新增一个配置文件,不需要修改原有配置:而且支持动 ...

  7. sqlite的bool字段

    简直被坑死了, bool字段更新,只能用0或1,才是正确的更新. 否则select出来的字段是错的 本来用true和false更新的,更新之后,使用sqliteexpert查看,更新结果是对的. 但是 ...

  8. SPOJ—VLATTICE Visible Lattice Points(莫比乌斯反演)

    http://www.spoj.com/problems/VLATTICE/en/ 题意: 给一个长度为N的正方形,从(0,0,0)能看到多少个点. 思路:这道题其实和能量采集是差不多的,只不过从二维 ...

  9. python 匹配指定后缀的文件名

    import glob x=glob.glob('*.py') print(x)

  10. [原][osgearth]earth文件加载道路一初步看见模型道路

    时间是2017年2月5日17:16:32 由于OE2.9还没有发布,但是我又急于使用OE的道路. 所以,我先编译了正在github上调试中的OE2.9 github网址是:https://github ...