The suspects
| Time limit:1000 ms |
|
Memory limit:20000 K |
| Total submissions:21472 |
|
Accepted:10393 |
Description
Severe Acute Respiratory Syndrome (SARS), an atypical ppneumonia onia 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 still 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
The input file contains several cases. each test case begins with two integers n and m in a line, where N is the number of students, and m is the number of groups. you may 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 is recognized as a suspect in all the cases. this line is followed by M member lists of the groups, one line per group. each line begins with an integer k by itself representing the number of members in the group. following the number of members, there are K integers representing the students in this group. all the integers in a line are separated by at least one space.
A case with n = 0 and m = 0 indicates the end of the input, and need not be processed.
Output
For each case, output the number of suspects in one line.
Sample Input
100 42 1 25 10 13 11 12 142 0 12 99 2200 21 55 1 2 3 4 51 00 0
Sample output
411
Source
Asia Kaohsiung 2003
Query set, classic question.
And query the set template (from Peking University courseware):
1 int UFS [maxn]; // query set 2 3 void Init (int n) // initialize 4 {5 Int I; 6 for (I = 0; I <N; I ++) {7 UFS [I] = I; 8} 9} 10 11 int getroot (int A) // obtain the root node of. Path compression 12 {13 if (UFS [a]! = A) {// The root node 14 UFS [a] = getroot (UFS [a]); 15} 16 return UFS [a]; 17} 18 19 void Merge (int A, int B) // merge the set of A and B 20 {21 UFS [getroot (B)] = getroot (); 22} 23 24 bool query (int A, int B) // query whether A and B are in the same set 25 {26 return getroot (A) = getroot (B); 27}
Question:
Ideas: This is a classic query of the set question. Find a sum [] array to record the number of individuals in each set whose current subscript is the root node, finally, the sum value corresponding to the root node of No. 0 is output, which is the number of students in the group of No. 0.
Code:
1 # include <iostream> 2 # include <stdio. h> 3 using namespace STD; 4 # define maxn 30010 5 Int sum [maxn]; // total number of sets 6 int UFS [maxn]; // query the set 7 8 void Init (int n) // initialize 9 {10 int I; 11 for (I = 0; I <n; I ++) {12 UFS [I] = I; 13 sum [I] = 1; 14} 15} 16 17 int getroot (int A) // obtain the root node of. Path compression 18 {19 if (UFS [a]! = A) {// The root node 20 UFS [a] = getroot (UFS [a]); 21} 22 return UFS [a]; 23} 24 25 void Merge (int A, int B) // merge the set of A and B 26 {27 int x = getroot (); 28 int y = getroot (B); 29 If (X! = Y) {30 UFS [y] = x; 31 sum [x] + = sum [y]; 32} 33} 34 35 int main () 36 {37 int N, m; 38 While (scanf ("% d", & N, & M )! = EOF) {39 if (n = 0 & M = 0) break; 40 Init (n); // initialize and query the set 41 while (M --) {// read m rows 42 int T, One, two; 43 scanf ("% d", & T ); // enter 44 scanf ("% d", & one); 45 t --; 46 While (t --) {47 scanf ("% d ", & two); 48 Merge (ONE, TWO); // merge set 49} 50} 51 printf ("% d \ n", sum [getroot (0)]); 52} 53 return 0; 54}
Freecode: www.cnblogs.com/yym2013