概述
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("%dn",sum); //输出
}
return 0;
}
最后
以上就是专注康乃馨为你收集整理的The Suspects(并查集)The Suspects(并查集)的全部内容,希望文章能够帮你解决The Suspects(并查集)The Suspects(并查集)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复