Title Address:
http://poj.org/problem?id=1611
Topic content:
The Suspects
Time Limit: 1000MS |
|
Memory Limit: 20000K |
Total Submissions: 24253 |
|
Accepted: 11868 |
Description
Severe Acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, were recognized as a global threat in Mid-March 2003. To minimize transmission to others, the best strategy are to separate the suspects from others.
In the Not-spreading-your-sickness University (NSYSU), there is many student groups. The 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 FO Llowing rule in their standard operation procedure (SOP).
Once a member in a group was a suspect, all members of the group are suspects.
However, they find that it's 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 integers n and m in a line, where n is the number of students, and M are the number of group S. 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 was followed by M member lists of the groups and one line per group. Each line begins with a integer k by itself representing the number of members in the group. Following the number of members, there is k integers representing the students in this group. All the integers in a line is separated by at least one space.
A case with n = 0 and M = 0 indicates the end of the input, and need not being 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 Problem-solving ideas: a little bit of change and check set, but still a water problem. The key is: No. 0, NO. 0 students must be ill. 1. The size of the collection must be recorded for output. So we merged each group of classmates and finally found the size of the 0 collection and output the results. When maintaining the collection size, you can maintain only the number of child nodes of the root node. For example, we use the array num to save the size of each collection, if 2,3,4,5 four people are in the same set, and 2 is the root node, then only the value of num[2] is meaningful, indicating the number of child nodes that 2 has. The rest of the num[3], num[4] equivalents are meaningless. And the size of this collection is num[2] + 1 All code:
#include <stdio.h>intman[30001];intnum[30001];//Records have several child nodes, but only the data of the root record is countedvoidInitintN) { for(inti =0; I < n; i + +) {Man[i]= -1; Num[i]=0; }}intFind_root (intChild ) { if(Man[child] = =-1) returnChild ; returnMan[child] =Find_root (Man[child]);}voidUnion_set (intOneintBoth ) { intFAT1 =Find_root (one); intFAT2 =Find_root (both); //printf ("%d and%d has been union.", FAT1, FAT2); if(FAT1 = =fat2)return; NUM[FAT1]+ = Num[fat2] +1; MAN[FAT2]=FAT1; //printf ("Father is%d\n", FAT1);}intMainvoid){ intM,n; while(SCANF ("%d%d", &n, &m)) {if(M = =0&& n = =0) Break; Init (n); for(inti =0; I < m; i + +) { intCount,pre,now; scanf ("%d", &count); Pre= -1; for(intj =0; J < Count; J + +) {scanf ("%d", &Now ); if(Pre! =-1) {Union_set (pre, now); } Pre=Now ; } } intFA = Find_root (0); printf ("%d\n", NUM[FA] +1); } return 0;}
"Original" POJ-----1611 The Suspects Problem solving report