B - The Suspects
POJ - 1611Severe 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.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
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
4 1 1
也是之前师哥让做过的一道题,直接贴不解释,直接挂模板就能过
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int m,n;
int d[50010];
int num[50010];
int find(int a)
{
if(a==d[a])
return d[a];
else
return d[a] = find(d[a]);
}
void join(int b,int c)
{
b=find(b);
c=find(c);
if(b==c)return ;
else
{
d[b]=c;
num[c]+=num[b];
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(m==0&&n==0)break;
for(int i = 0; i < n; i ++)
{
d[i]=i;
num[i]=1;
}
int j,k,l;
for(int i = 0; i < m; i ++)
{
scanf("%d",&k);
scanf("%d",&j);
for(int p = 0; p < k - 1; p ++)
{
scanf("%d",&l);
join(j,l);
}
}
printf("%d\n",num[find(0)]);
}
return 0;
}