The suspectstime limit:1000ms Memory limit:20000kb 64bit IO Format:%LLD &%lludescription Severe Acute respiratory syndrome (SARS), an unknown cause of atypical pneumonia, was considered a global threat since mid-March 2003. In order to reduce the chance of spreading to others, the best strategy is to isolate possible patients. In not-spreading-your-Sickness University (NSYSU), there are many student groups. Students in the same group are often connected to each other, and a student can join several groups at the same time. To prevent the spread of SARS, Nsysu collected a list of members of all student groups. Their standard operating procedures (SOP) are as follows: Once a group has one possible patient, all members within the group are likely patients. However, they found that when a student is identified as a possible patient, it is not easy to identify all possible patients. Your job is to write a program that discovers all possible patients. The input file contains multiple sets of data. For each set of test data: The first behavior is two integers n and m, where N is the number of students, and M is the number of groups. 0< n <=30000,0<= m <= -. Each student number is a 0 to n-an integer between 1 and a beginning only No. 0 students are considered to be possible patients. followed by a list of members of the group, one row per group. Each line has an integer k, which represents the number of members. After that, there are k integers representing this group of students. All integers in a row are separated by at least one space. N= m =0 means the input ends and no processing is required. Output for each set of test data, a single line of possible patients is exported. Sample Input - 42 1 25 Ten - One A -2 0 12 About 2 $ 21 55 1 2 3 4 51 00 0Sample Output411
Simple and check-set
In each circle of the first artificial root node, all the people's parent node is marked as the root node, and then record the number of the root node;
When two nodes have root nodes, and the root node is different, merge two circles, change the root node to one, update the number of the root node at this time.
AC Code:
#include "iostream" #include "Cstdio" #include "algorithm" #include "Cmath" #include "CString" using namespace std; #define MX 1000000int pe[mx];int num[mx];int Find (int x) {return pe[x] = = X ×: (Pe[x] = find (pe[x])); } int main () {int n,m,x;while (~scanf ("%d%d", &n,&m)) {if (!n&&!m) break;for (int i=0; i<n; i++) {pe[i]=i; num[i]=1;//marks each person as the number of child nodes of the root node}for (int qq=1; qq<=m; qq++) {scanf ("%d", &x), int st[x];memset (st,0,sizeof st); for ( int i=0; i<x; i++) {scanf ("%d", &st[i]), if (i>=1) {int root1,root2;root1=find (st[i-1]); Root2=find (St[i]); if (root1! = Root2) { If the root node of two people is different pe[root2] = root1;//The root node of the second circle into the root node of the first circle num[root1]+=num[root2];//the number of people with the same root node is added to the root node}}}}printf ("%d\n", Num[find (0)]);//Direct search for 0 of the root node number}}
Acm:the suspects--Problem Solving report