The Suspects (and the number of nodes to find)

Source: Internet
Author: User

The Suspects
Time Limit: 1000MS Memory Limit: 20000K
Total Submissions: 28164 Accepted: 13718

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
Code:
1#include <stdio.h>2#include <string.h>3 Const intmaxn=30010;4 intPRE[MAXN],NUM[MAXN];5 intFindintx) {6     returnPRE[X]=X==PRE[X]?X:find (pre[x]);7 }8 intMergeintXinty) {9     intF1,f2;TenF1=find (x); f2=find (y); One     if(f1!=F2) { A         if(F1&LT;F2) pre[f2]=f1,num[f1]+=NUM[F2]; -         Elsepre[f1]=f2,num[f2]+=NUM[F1]; -     } the } - intMain () { -     intn,m,k,a,b; -      while(SCANF ("%d%d", &n,&m), n|m) { +          for(intI=0; i<n;i++){ -pre[i]=i; +num[i]=1; A         } at          while(m--){ -scanf"%d",&k); -scanf"%d",&a); -              for(intI=1; i<k;i++){ -scanf"%d",&b); - merge (A, b); in             } -         } toprintf"%d\n", num[0]); +     } -     return 0; the}

The Suspects (and the number of nodes to find)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.