POJ - 1611 The Suspects
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
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
并查集,求出与0号学生有社团关系交际的人数(包含他自己)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 5 using namespace std; 6 7 int par[30005],a[30005]; 8 9 int find(int x) 10 { 11 if(par[x]!=x) 12 par[x]=find(par[x]); 13 return par[x]; 14 } 15 16 void mix(int x,int y) 17 { 18 int xx=find(x); 19 int yy=find(y); 20 if(xx!=yy) 21 par[yy]=xx; 22 } 23 24 int main() 25 { 26 int n,m,k; 27 while(~scanf("%d%d",&n,&m),n||m) 28 { 29 for(int i=0;i<n;i++) 30 par[i]=i; 31 for(int i=0;i<m;i++) 32 { 33 scanf("%d",&k); 34 scanf("%d",&a[0]); 35 for(int j=1;j<k;j++) 36 { 37 scanf("%d",&a[j]); 38 mix(a[0],a[j]); 39 } 40 } 41 int sum=0; 42 for(int i=0;i<n;i++) 43 if(find(i)==par[0]) 44 sum++; 45 printf("%d\n",sum); 46 } 47 48 49 return 0; 50 }

浙公网安备 33010602011771号