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
解析:只要是某组数据含有0,就让0作为根节点,然后记录数的节点数。
#include<stdio.h>
const int M=50000;
int par[M],vis[M];
int find(int x)
{
return x==par[x]?x:par[x]=find(par[x]);
}
void unite(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy){
if(fx==0){
vis[0]+=vis[fy];
par[fy]=fx;
}
else if(fy==0){
vis[0]+=vis[fx];
par[fx]=fy;
}
else{
vis[fx]+=vis[fy];
par[fy]=fx;
}
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)&&(n||m)){
for(int i=0;i<=n;i++){
par[i]=i;
vis[i]=1;
}
int t,x,y;
while(m--){
scanf("%d",&t);
scanf("%d",&x);
for(int i=1;i<t;i++){
scanf("%d",&y);
unite(x,y);
}
}
printf("%d\n",vis[0]);
}
}
1347



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



