The Suspects(并查集)

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.

翻译:

严重急性呼吸系统综合征(sars)是一种病因不明的非典型肺炎,2003年3月中旬被认为是一种全球性威胁。为了尽量减少传染给他人,最好的策略是把嫌疑犯与他人分开。
在不传播你的疾病大学(NSYSU),有许多学生团体。同一组的学生经常互相交流,一个学生可以加入几个组。为防止非典型肺炎的传播,南科大收集所有学生团体的成员名单,并在其标准操作程序(SOP)中制定以下规则。
一旦一个组中的成员是嫌疑犯,该组中的所有成员都是嫌疑犯。
然而,他们发现,当一个学生被认定为嫌疑犯时,要找出所有的嫌疑犯并不容易。你的工作是写一个程序来找到所有的嫌疑犯。

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.

翻译:

输入文件包含多个案例。每个测试用例以两个整数n和m开始,其中n是学生数,m是组数。你可以假设0<n<=30000和0<=m<=500。每个学生都由一个介于0和n-1之间的唯一整数进行编号,最初学生0在所有情况下都被视为嫌疑犯。这一行后面是m个组的成员列表,每个组一行。每行以一个整数k开头,代表组中的成员数。在成员数之后,有k个整数表示该组中的学生。一行中的所有整数至少由一个空格分隔。
n=0和m=0的情况表示输入结束,不需要处理。

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<cstdio>
#include<iostream>
using namespace std;
int m,n,i,j,k;
int stu[33333],par[33333];
void init()                  //初始化数组,每个节点的父节点都是自己
{for(i=0;i<n;i++)par[i]=i;
}
int find(int x)            //查询并进行路径压缩
{return par[x]==x?x:par[x]=find(par[x]);}
/* 方法二   十分不推荐    因为我也没看懂
int find(int x)       //查询父节点,直到没有父节点 ,并进行路径压缩,即将所有的点归于同一父节点下
{int r=x;while(par[r]!=r)r=par[r];int j,i=x;while(par[i]!=r){j=par[i];par[i]=r;i=j;}return r;
}*/void Union(int x,int y)        //将两个不相干的点连接起来
{                              //让其中一方(或其父节点)成为另一点(或其父节点)的父节点 int p1 = find(x);int p2 = find(y);if(p1!=p2)par[p1]=p2;
}
int main()
{while(scanf("%d%d",&n,&m)!=EOF)        //输入学生数跟组数 {int sum=0;if(m==0&&n==0)            //判断是否结束 break;init();               //初始化 for(i=0;i<m;i++){scanf("%d",&k);   //输入小组中的人数 int a,b;scanf("%d",&a);  //输入一个组员的编号 for(j=0;j<k-1;j++){scanf("%d",&b);   //输入另一个组员的编号 Union(a,b);       //连接 a=b;              //更新 }} for(i=0;i<n;i++)     //判断有多少嫌疑犯,即有多少编号与 0 是同一个父节点 if(find(i)==par[0])sum++;printf("%d\n",sum);     //输出 }return 0;
}

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. The Suspects//并查集

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

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

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

  6. 【转】并查集MST题集

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

  7. POJ 1611 The Suspects (并查集)

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

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

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

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

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

最新文章

  1. JS面向对象高级特性
  2. 打开CMDLINE中的 ” earlyprink “ 参数
  3. TimeQuest学习总结
  4. mongodb远程连接windows
  5. oracle存储过程转mysql存储过程修改方法
  6. 怎样使用 ASP.NET Optimization Bundling压缩样式表和脚本
  7. ios开发-Object-C可变参数函数
  8. python—单例模式与多例模式的区别与创建
  9. freemarker【FTL】常见语法大全
  10. Manitest: Are classifiers really invariant?论文解读
  11. 人人网普通登录源码爬取
  12. Android逆向之路---让我们试试另一种方法看漫画-(1)
  13. 欧洲航天局遭匿名者(Anonymous)攻击泄露大量数据
  14. 游戏美术资源网站推荐
  15. 云南大学计算机在职硕士,在职硕士
  16. fragment实例
  17. appcms手机端点击按钮返回顶部
  18. css实现图片毛玻璃效果
  19. MyBatis-Plus--自动填充的用法
  20. matlab移相法实现单边带调制,移相法实现单边带信号的调制

热门文章

  1. oracle是前端还是后端,2021年前端vs后端 哪个就业前景更好?
  2. 浙江万里学院c语言考试题库,浙江万里学院单片机原理及应用模拟试卷八
  3. STM32CubeMX驱动MPU6050模块
  4. hana数据库同步到mysql中
  5. try中有return,finally还会执行吗?
  6. 【分享】手机测试之详谈
  7. 初中英语多词性单词怎么办_初中英语单词词性归类
  8. matlab 索引超出矩阵维度
  9. 常见的互联网数据指标
  10. C语言实现稀硫矩阵的三元组表示及其转置