题目:
The Suspects
Time Limit: 1000MS   Memory Limit: 20000K
Total Submissions: 48312   Accepted: 23113

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

100 4
2 1 2
5 10 13 11 12 14
2 0 1
2 99 2
200 2
1 5
5 1 2 3 4 5
1 0
0 0

Sample Output

4

1

1

思路:

代码:

#include <iostream>int Find(int a[], int x)//递归的路径压缩
{if(x == a[x])return x;return a[x] = Find(a,a[x]);
}bool IsConnected(int a[], int x, int y)
{return Find(a,x) == Find(a,y);
}void Union(int a[], int b[], int x, int y)
{int c = Find(a,x);int d = Find(a,y);a[c] = d;b[d] += b[c];return;
}int main(void)
{using namespace std;int n, m;while(cin >> n >> m && (n != 0 || m != 0)){int * a = new int[n];int * b = new int[n];for(int i = 0; i < n; i++){a[i] = i;b[i] = 1;}while(m--)//m行
        {int line;cin >> line;int * group = new int[line];for(int i = 0; i < line; i++)//输入一行数据到数组group中cin >> group[i];for(int i = 1; i < line; i++){if(IsConnected(a,group[0],group[i]))continue;Union(a,b,group[0],group[i]);}delete[] group;}int ans = b[Find(a,0)];printf("%d\n", ans);delete[] a;delete[] b;}return 0;
}

转载于:https://www.cnblogs.com/w-j-c/p/9218935.html

The Suspects//并查集相关推荐

  1. POJ 1611 The Suspects 并查集

    The Suspects Time Limit:1000MS     Memory Limit:20000KB     64bit IO Format:%lld & %llu Descript ...

  2. poj 1611 The Suspects // hoj 1564 The Suspects 并查集

    /* 题目: 是说学生0怀疑有SARS病,跟他接触过的俱乐部的所有人以及他接触过的人再与别人接触, 都有可能有SARS病,要你求出给出的所有俱乐部人的名单,要你求出所有的嫌疑犯... 分析: 用并查集 ...

  3. 感冒病毒 suspects 并查集

    题目大意:给定n个人,m个团,同一个团中只要有一个人感染病毒整个团就都感染病毒.现在0号感染了病毒,问总共有多少个人会感染病毒. 题目分析:简单的并查集. #include<cstdio> ...

  4. pku 1611 The Suspects 并查集的应用

    http://poj.org/problem?id=1611 思路:统计出和0能够联系在一起的点,然后输出其个数 View Code #include <cstdio>#include & ...

  5. 【转】并查集MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 ...

  6. POJ 1611 The Suspects (并查集)

    The Suspects 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/B Description 严重急性呼吸系统综合症( S ...

  7. B - The Suspects(并查集)详解

    题目描述:n个学生分属m个团体,一个学生可以属于多个团体.一个学生疑似患病则它所属的整个团体都疑似患病.已知0号学生疑似患病,以及每个团体都由哪些学生构成,求一共多少个学生疑似患病. 解题思路:本题的 ...

  8. 【并查集】感冒病毒 suspects

    感冒病毒 suspects.pas/c/cpp 1S/256MB [题目描述] 一种感冒病毒正在学校里传播,这所学校有n个学生,m个学生社团,每个学生可能参加了多个社团,因为同一个社团的学生交流较多, ...

  9. The Suspects(并查集)

    The Suspects(并查集) Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiolo ...

最新文章

  1. 一、.NET中的网络组件
  2. ubuntu下gcc的安装与使用
  3. Keywords Search HDU - 2222(AC自动机模板)
  4. activiti自定义_在Activiti中执行自定义查询
  5. 在head标签内css代码,怎么在head区域引入css
  6. python2.7 安装第三方库
  7. python nodemcu_python开发nodemcu2(跑马灯实验)
  8. 钢结构计算机模拟拼装,钢结构技术-钢结构虚拟预拼装技术
  9. 如何在Windows下像Mac一样优雅开发
  10. 连接NI采集箱9181与9184
  11. DBC文件编写概述 及 CANoe 使用DBC分析信号
  12. 系统迁移工具迁移操作系统到别的硬盘
  13. ABBYY FineReader添加盖章戳记
  14. wpf自己画表盘_练手WPF(一)——模拟时钟与数字时钟的制作(上)
  15. 浅谈测试工程师必备软技能:沟通表达篇
  16. 使用 natbib 进行参考文献管理
  17. 如何将台式机的网络共享给另外一台台式机
  18. 支付宝崩了,七夕红包发得太多?
  19. 三国志战略版:三势贾的另类搭配,也可以这么强?
  20. 海外试玩推广渠道汇总

热门文章

  1. GDCM:区分音量DiscriminateVolume的测试
  2. Boost:在GPU上对整数向量进行排序
  3. ITK:在图像上叠加标签图
  4. ITK:处理矢量图像的N个分量
  5. OpenCV equalizeHist直方图均衡化的实例(附完整代码)
  6. C语言链表返回第n个到最后的节点的算法(附完整源码)
  7. C++Kosaraju找有向图的强连通分量算法(附完整源码)
  8. c++引用专题之常引用
  9. C++11- const, const expression和constexpr
  10. AttributeError: module ‘tensorflow‘ has no attribute ‘placeholder‘