题目

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

分析与解答

通过改了一个连接函数join,我们的根的num直接储存他所在的树的结点个数,由于题目说0号事已经有了,那我们找到0号的根然后输出跟的num即可

#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 33000
using namespace std;int per[maxn], num[maxn], n, m;
int a[maxn];int find(int x){if(x == per[x])return x;return per[x] = find(per[x]);
}void join(int x, int y){int fx = find(x);int fy = find(y);if(fx != fy){per[fx] = fy;//把fy规定为fx祖宗 num[fy] = num[fx]+num[fy];//fy所在的数的结点的个数(包含他自己) }return ;
}int main (){while(scanf("%d%d", &n, &m)){if(n==0&&m==0) return 0;for(int i = 0; i < n ; ++i){per[i] = i;num[i] = 1;}while(m--){int t;scanf("%d", &t);for(int i = 0; i < t; ++i){scanf("%d", &a[i]);}for(int i = 0 ; i < t - 1; ++i)join(a[i], a[i + 1]);}int  k = find(0);printf("%d\n", num[k]);}return 0;
}

(并查集)The Suspects相关推荐

  1. POJ 1611 The Suspects (并查集)

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

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

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

  3. POJ 1611 The Suspects 并查集

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

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

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

  5. The Suspects(并查集)

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

  6. POJ 1611 The Suspects (并查集)

    文章作者:ktyanny 文章来源:ktyanny 转载请注明,谢谢合作. ktyanny:a的第一道并查集. 题目描述: 有很多组学生,在同一个组的学生经常会接触,也会有新的同学的加入.但是SARS ...

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

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

  8. The Suspects(并查集入门)

    题目:http://www.fjutacm.com/Problem.jsp?pid=2021 题意大概就是输入n,m,分别代表总共n个人,m组,每组输入k,后面再输入k个人表示是一组的,0号是嫌疑者, ...

  9. 感冒病毒 suspects 并查集

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

  10. 树形结构 —— 并查集

    [概述] 并查集(Union-Find Set)是一种用于分离集合操作的抽象数据类型,其处理的是集合(set)之间的关系,一般处理的是图的连通分量,当给出两个的元素的一个无序对 (a,b) 时,需要快 ...

最新文章

  1. cylance做的机器学习相关材料汇总
  2. Delphi编码及注释规范
  3. 一起学nRF51xx 15 - spis
  4. Mybatis 的工作原理,写得太好了!
  5. 动图处理_面对单刀球的几种处理方法(动图演示)
  6. gparted在线扩分区大小
  7. typescript的命名空间
  8. 计算机系统如何禁止删除文件,电脑禁止安装删除文件 禁止复制电脑文件的方法...
  9. 用Docker快速搭建一个博客网站,很简单的嘛~
  10. 【待完善】【表达学习】稀疏表达SRC方法研究
  11. 记腾讯一面 | 掘金技术征文
  12. 力扣题目——637. 二叉树的层平均值
  13. java版的mrp模拟器_mrp模拟器(simulator)
  14. 菲尔茨奖得主陶哲轩:瓜分数学成就 没有益处
  15. QT系列之曲线图绘制(推荐QCustomPlot)
  16. 如何使用Pixelmator Pro处理图片?mac pixelmator使用教程
  17. 用计算机制作多媒体作品使用的素材都必须,制作多媒体作品(选择题).doc
  18. PE格式的定义头文件winnt.h
  19. IMAGE WARPING (IDW+RBF)
  20. 国内外知名源码商城系统盘点

热门文章

  1. python lxml xpath爬取图片代码
  2. 想要入坑机器学习?这是MIT在读博士的AI心得
  3. 清浊音判别 matlab,matlab语音信号处理如何判别清浊音?
  4. oracle未授权sql查询,【oracle使用笔记3】sql查询遇到的若干问题总结
  5. AngularJS小结
  6. Vue 打包前需修改的配置,解决白屏问题
  7. vue生命周期详解、钩子函数的调用(简单易懂)
  8. spring中context:property-placeholder标签详解
  9. mysql80配置环境变量_MySQL:安装与配置
  10. extjs 渲染之前的方法_Unity通用渲染管线(URP)系列(十一)——后处理(Bloom)...