| Time Limit: 1000MS | Memory Limit: 20000K | |
| Total Submissions: 7748 | Accepted: 3674 |
Description
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
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
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
Source
#include<stdio.h>
int n,m,k;
int father[30010],num[30010];
int getfather(int x)
{
if(x!=father[x]) father[x]=getfather(father[x]);
return father[x];
}
void Union(int a,int b)
{
a=getfather(a);
b=getfather(b);
if(a==b)
return;
if(num[a]<num[b])
{
father[a]=b;
num[b]+=num[a];
}
else
{
father[b]=a;
num[a]+=num[b];
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0) break;
for(int i=0;i<n;i++)
{
father[i]=i;
num[i]=1;
}
while(m--)
{
int x;
scanf("%d",&k);
scanf("%d",&x);
for(int i=2;i<=k;i++)
{
int y;
scanf("%d",&y);
Union(x,y);
}
}
printf("%d/n",num[getfather(0)]);
}
return 0;
}
SARS传播模拟算法
本文介绍了一种通过学生群体间关系来模拟SARS潜在传播范围的算法。该算法使用并查集实现,针对每个已知感染者所在的群体进行成员标记,以此来确定所有潜在的感染人群。
11万+

被折叠的 条评论
为什么被折叠?



