The Suspects
Time Limit: 1000MS |
|
Memory Limit: 20000K |
Total Submissions: 24134 |
|
Accepted: 11787 |
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 topics and algorithm analysis: There are n individuals, number 0---n-1,m groups, enter the number of each group of M group, each group is a small collective. Suppose No. 0 is a suspect, and all suspects in a group are suspects. Note: If 1 and 0 are in a group, and 1 and 2, 3, 4 and so on in a group, these people are also suspects! It is necessary to set up an array of No. 0 numbers to carry out statistical preservation of the number of descendants of each node by using the algorithm of merging and checking. Code:
1#include <stdio.h>2#include <string.h>3#include <stdlib.h>4#include <math.h>5#include <ctype.h>6#include <iostream>7#include <string>8#include <iomanip>9#include <algorithm>Ten #defineN 30000 One A using namespacestd; - intN, M; - intCnt[n]; the intFa[n]; - - //0 < n <= 30000 and 0 <= m <=500 - voidInit () + { - for(intI=0; i<n; i++) + { Afa[i]=i; atcnt[i]=1; - } - } - - intFindset (intx) - { in returnFa[x]!=x? fa[x]=Findset (fa[x]): x; - } to + voidUnion_set (intXinty) - { the intxx=Findset (x); * intyy=Findset (y); $ if(XX==YY)return;//description Two elements are originally part of the same collection returnedPanax Notoginseng Else if(XX<YY)//if the root node of x is smaller than the root node of y - { thefa[yy]=xx; +cnt[xx]=cnt[xx]+Cnt[yy]; A } the Else if(xx>yy) + { -fa[xx]=yy; $cnt[yy]=cnt[yy]+Cnt[xx]; $ } - } - the intMain () - {Wuyi intDD, A, B; the while(SCANF ("%d%d", &n, &m)! =EOF) - { Wu if(n==0&& m==0) Break; - init (); About for(intI=0; i<m; i++) $ { -scanf"%d", &dd); -scanf"%d", &a); - for(intj=0; j<dd-1; J + +) A { +scanf"%d", &b); the Union_set (A, b); -A=b; $ } the } theprintf"%d\n", cnt[0] ); the } the return 0; -}
View Code
POJ 1611 The Suspects (and the number of descendants of the Collection + array record)