概述
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<stdio.h>
int a[30005] ,group[30005], num[30005];
int findgroup(int x)//查询每个学生的组长是谁。
{
if(x==group[x]) return x;
else return findgroup(group[x]);
}
void join(int x,int y)//在同一组的学生需要把组长选定。
{
int grox=findgroup(x);
int groy=findgroup(y);
if(grox!=groy)
{
group[grox]=groy;
num[groy]+=num[grox];//每一次合并,都需要吧集合中的元素个数加进去。
}
return ;
}
int main()
{
int n,m,k;
while(~scanf("%d %d",&n,&m))
{
if(n==0&&m==0)
break;
for(int i=0; i<n; i++) //开个数组,给数组赋值,即自己自成一组并为组长。
{
group[i]=i;
num[i]=1;
}
while(m--)//共有m个样例。
{
scanf("%d ",&k);
for(int i=0; i<k; i++)
scanf("%d",&a[i]);
for(int i=0; i<k-1; i++)//把在同一组的几个学生合并在一起。
join(a[i],a[i+1]);//如果组长不同,便把组长选定。
}
int t=findgroup(0);//找到生病的0号学生的组长。
printf("%dn",num[t]);
}
return 0;
}
题目大意:为防止传染病的传播,需要将健康人和疑似患病的人分隔开。第一行输入两个整数n,m;n代表的为学生总数,m代表的为校内的小组数,n个学生的编号为0—n-1,接下来的m行首先输入一个整数,代表该组的学生数,然后一次输入该组学生的编号(在输入时,两个数之间用一个空格隔开)。已知0号学生患病,并且与0为同一组的学生都疑似患病。问需要隔离的疑似患病学生人数。
思路:知道并查集的三个基本操作就会好做一些。一是初始化,即让每个元素都是一个独立的集合。二是查找,即对给定的结点寻找其根节点的过程(实现方式有递推和递归)。三是合并,即把两个不同集合的元素合并到同一个集合。本质上就是用了一个数组。
查找的两种方式:
递推:
//findFather函数返回元素x所在集合的根节点。
int findFather(int x)
{
while(x!=father[x])//如果不是根节点,继续循环。
{
x=father[x];//获得自己的父亲结点。
}
return x;
}
递归:
//findFather函数返回元素x所在集合的根节点。
int findFather(int x)
{
if(x==father[x]) return x;//如果找到根节点,则返回根节点编号x。
else return findFather(father[x]);//否则,递归判断x的父亲结点是否是根节点。
}
合并的代码:
void Union(int a,int b)
{
int faA=findFather(a);//查找a的根结点,记为faA。
int faB=findFather(b);//查找b的根结点,记为faB。
if(faA!=faB)//如果不属于同一个集合。
{
father[faA]=faB;//合并它们。
}
}
面对英文题目满满的绝望,要么提高英语,要么自己挨个有道翻译,没招。
最后
以上就是乐观柜子为你收集整理的并查集—B - The Suspects的全部内容,希望文章能够帮你解决并查集—B - The Suspects所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复