POJ-1611-The Suspects (simple and query set !), Query set

Source: Internet
Author: User

POJ-1611-The Suspects (simple and query set !), Query set

The Suspects
Time Limit:1000 MS   Memory Limit:20000 K
Total Submissions:23337   Accepted:11345

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



The first time I did and checked the set, I couldn't find it. Then I checked the Baidu encyclopedia and found the set. It seems I understood it, but it is not the same in use.

There are many groups of students in the same group who often contact each other and add new students to the group. However, SARS is very contagious. As long as a student in the reorganization is infected with SARS, all the students in the group are considered to have had SARS. The task is to calculate how many students are infected with SARS. Assume that the number 0 is SARS.

Train of Thought: Check the set and read the code in detail!


AC code:

# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> using namespace std; const int maxn = 30005; int fa [maxn], rank [maxn]; // fa [] is the root node, and rank [] is the height of the root node int find (int x) // query the root node {if (x! = Fa [x]) fa [x] = find (fa [x]); return fa [x];} int main () {int n, m, k; while (scanf ("% d", & n, & m), n | m) {int a, B; for (int I = 0; I <n; I ++) {fa [I] = I; rank [I] = 0;} if (m = 0) {printf ("1 \ n"); continue ;} while (m --) // enter each set {scanf ("% d", & k); k --; scanf ("% d", & ); while (k --) {scanf ("% d", & B); a = find (a); B = find (B ); if (rank [a]> rank [B]) // if the height is higher, the root node {fa [B] = a;} else {fa [a] = B; if (rank [a] = rank [B]) rank [B] ++;} a = B ;}} int ans = 0, x = find (0 ); for (int I = 0; I <n; I ++) {if (find (I) = x) ans ++; // fa [I] = x cannot be written here, and an error will occur. You must update the root node again.} printf ("% d \ n", ans );} return 0 ;}




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.