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
题意:
模板在这----套模板~~~
- 有很多组学生,在同一个组的学生经常会接触,也会有新的同学的加入。
- 但是SARS是很容易传染的,只要在改组有一位同学感染SARS,那么该组的所有同学都被认为得了SARS。
- 现在的任务是计算出有多少位学生感染SARS了。假定编号为0的同学是得了SARS的。
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=30002;
int height[maxn]; //用height[i]定义元素i的高度
int n,m;
int s[maxn];
int ans;
int find_set(int x)
{
int r = x;
while ( s[r] != r ) r=s[r]; //找到根结点
int i = x, j;
while(i != r)
{
j = s[i]; //用临时变量j记录
s[i]= r ; //把路径上元素的集改为根结点
i = j;
}
return r;
}
void init_set()
{
for(int i = 0; i < n; i++)
{
s[i] = i;
height[i]=0; //初始化树的高度
}
}
void union_set(int x, int y) //优化合并操作
{
x = find_set(x);
y = find_set(y);
if (height[x] == height[y])
{
height[x] = height[x] + 1; //合并,树的高度加1
s[y] = x;
}
else //把矮树并到高树上,高树的高度保持不变
{
if (height[x] < height[y]) s[x] = y;
else s[y] = x;
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(m==0&&n==0)
break;
ans=0;
init_set();
while(m--)
{
int k;
scanf("%d",&k);
int a;
scanf("%d",&a);
for(int i=1; i<k; i++)
{
int x;
scanf("%d",&x);
union_set(x,a);
}
}
for(int i=0; i<n; i++)
{
if(find_set(i)==s[0])
{
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
本文介绍了一种用于模拟SARS病毒在大学校园内潜在传播情况的算法。通过将学生按组划分并应用并查集数据结构,算法能够快速确定所有潜在感染者。此方法对于疫情初期的快速响应具有重要意义。
285





