The suspects time limit: 1000ms Memory limit: 20000k total submissions: 32929 Accepted:&nbs p;15965
Description Severe Acute respiratory syndrome (SARS), an atypical pneumonia-unknown aetiology, was recognized as a G Lobal threat in mid-march 2003. To minimize transmission to others, the best strategy are to separate the suspects from others.
in the Not-sprea Ding-your-sickness University (NSYSU) There are many student. Students in the same group Intercommunicate with each of the 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 FO Llowing rule in their standard operation procedure (SOP) .
Once A and a group is a suspect The group are suspects.
However, they find that it isn't easy to identify all the suspects when a student is Recognized as a suspect. Your job is to write a which finds the suspects.
Input the input file contains several cases. Each test case begins with two integers n and m at a line, where n is the number of students, and M is the number of group S. You could 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 are recognized as a suspect in all The cases. This are followed by M/lists of the groups, one line per group. The begins with a integer k by itself representing the number of the group. Following the number of members, there are k integers representing the students in this group. All of the integers in a line are separated by at least one.
A case with n = 0 and M = 0 indicates the "End of" input, and need not is processed.
Output for each case, output the number of suspects in one line.
Sample Input
4
2 1 2 5 a 2 0 1 2 a 2
2
1 5 5 1 2 3 4 5 1 0 0 0
Sample Output
4
1
1
Source
Asia Kaohsiung 2003
Meaning
There is a school, there are N students, numbered 0 to N-1, now No. 0 students infected with SARS, and 0 in a community of people will be infected, and these people if they also participate in other societies, his community is all infected, for the number of infections.
Analysis:
And check the deformation of the set, as long as the 0 students related, indirect related are considered possible infection; In each group input, the first classmate does not consider, the back of each classmate and the first classmate comparison, grouped into the same set, the last look and father[i]==father[0] a few, that is to ask:
Code:
#include <stdio.h> #include <algorithm> #include <string.h> #include <math.h> #include
<iostream> using namespace std;
#define N 50000 int father[n];
int n,m;
int find (int x) {while (x!=father[x]) x=father[x];
return x;
} void Join (int a,int b) {int x,y;
X=find (a);
Y=find (b);
if (x!=y) {if (x>y) father[y]=x;
else father[x]=y;
int main () {int i,j,k;
int a,b;
while (scanf ("%d%d", &n,&m), m+n) {for (i=0; i<n; i++) father[i]=i;
For (I=1 i<=m; i++) {scanf ("%d%d", &k,&a);
for (j=1;j<k;j++) {scanf ("%d", &b);
Join (A,B);
} int ans=0;
int P=find (0);
for (i=0;i<n;i++) if (Find (i) ==p) ans++;
printf ("%d\n", ans);
return 0; }