题意:有m组,0为起点,有0的那一组全是嫌疑人,之后会不断传递到其它组去,问一共有多少人是嫌疑人

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
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<math.h>
  5. #include<stdlib.h>
  6. #include<functional>
  7. #include<string>
  8. #include<queue>
  9. #include<set>
  10. #include<map>
  11. #include<vector>
  12. #include<algorithm>
  13. using namespace std;
  14. #define ll long long
  15. #define inf 0x3f3f3f3f
  16. int n,m,pre[30005],a[30005];
  17. int find(int x)
  18. {
  19. return x==pre[x]?x:pre[x]=find(pre[x]);
  20. }
  21. void merge(int x,int y)
  22. {
  23. int fx=find(x);
  24. int fy=find(y);
  25. if(fx!=fy)
  26. pre[fx]=fy;
  27. }
  28. int main()
  29. {
  30. while(cin>>n>>m&&(n+m))
  31. {
  32. for(int i=0;i<n;i++)//每人一个父节点
  33. pre[i]=i;
  34. for(int i=0;i<m;i++)
  35. {
  36. int k;
  37. cin>>k;
  38. cin>>a[0];
  39. for(int j=1;j<k;j++)
  40. {
  41. cin>>a[j];
  42. merge(a[0],a[j]);
  43. //因为每一行都是有关系的,
  44. //将每一行关联起来。
  45. }
  46. }
  47. int ans=0;
  48. for(int i=0;i<n;i++)
  49. {
  50. if(find(i)==pre[0])//判断0和第i个人是否有关系
  51. ans++;
  52. }
  53. printf("%d\n",ans);
  54. }
  55. }

A - The Suspects (sars传染)的更多相关文章

  1. 并查集模板题(The Suspects )HZNU寒假集训

    The Suspects Time Limit: 1000MS Memory Limit: 20000KTotal Submissions: 36817 Accepted: 17860 Descrip ...

  2. The Suspects——Asia Kaohsiung 2003

    原创 The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 48698 Accepted: 23286 Des ...

  3. POJ——1611The Suspects(启发式并查集+邻接表)

    The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 31100 Accepted: 15110 Descri ...

  4. 并查集 (Union Find ) P - The Suspects

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  5. [并查集] POJ 1611 The Suspects

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

  6. poj-1611-The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 34284   Accepted: 16642 De ...

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

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

  8. Poj1611The Suspects

    A - The Suspects Time Limit: 1000 MS Memory Limit: 20000 KB 64-bit integer IO format: %I64d , %I64u  ...

  9. poj 1611 The Suspects 并查集

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

随机推荐

  1. Linux设置系统时区

    https://www.xlsys.cn/1741.html 如果你的 Linux 系统时区配置不正确,必需要手动调整到正确的当地时区.NTP 对时间的同步处理只计算当地时间与 UTC 时间的偏移量, ...

  2. Lniux 入门:03 用户及文件权限管理

    1.1 实验内容 Linux 中创建.删除用户,及用户组等操作. Linux 中的文件权限设置. 1.2 实验知识点 Linux 用户管理 Linux 权限管理 通过第一节课程的学习,你应该已经知道, ...

  3. 相同的class的各位object互为友元(friend)

    相同的class的各位object互为友元(friend) 这句话是啥意思? 我们来看一段代码: 1 class complex{ 2 3 private: 4 5 int r,i; 6 public ...

  4. awk -v参数

    -v var=val --assign var=val Assign the value val to the variable var, before execution of the progra ...

  5. 【Linux】rsync 守护进程的配置

    环境 centos7.2 1.首先查看是否安装rsync的相关包 rpm -qa | grep rsync rsync-3.1.2-4.el7.x86_64 如果没安装就yum install rsy ...

  6. MAVEN编译NIFI源码

    场景: 由于项目需求,需要借用NIFI进行二次开发,因此需要将NIFI源码进行修改,然后编译,办公环境无外网. 步骤: (1)   找一台可以上网(外网)的机器,安装java环境和maven环境,安装 ...

  7. 分布式系统:分布式任务调度xxl-job较深入使用

    目录 系统关键概念介绍 执行器 任务 任务配置项描述 阻塞策略 路由策略 日志问题 客户端日志 服务端日志 框架目前发现的缺点以及存在的问题 xxl-job是一个分布式定时任务调度框架,功能强大,底层 ...

  8. Redis 实战 —— 02. Redis 简单实践 - 文章投票

    需求 功能: P15 发布文章 获取文章 文章分组 投支持票 数值及限制条件 P15 如果一篇文章获得了至少 200 张支持票,那么这篇文章就是一篇有趣的文章 如果这个网站每天有 50 篇有趣的文章, ...

  9. POJ1629:picnic planning

    题目描述 矮人虽小却喜欢乘坐巨大的轿车,轿车大到可以装下无论多少矮人.某天,N(N≤20)个矮人打算到野外聚餐.为了 集中到聚餐地点,矮人A 有以下两种选择 1)开车到矮人B家中,留下自己的轿车在矮人 ...

  10. 使用存储过程在mysql中批量插入数据

    一.在mysql数据库中创建一张表test DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` INT (11), `name` VARCH ...