Link:
http://poj.org/problem?id=1611
Topic:
The suspects
| Time limit:1000ms |
|
Memory limit:20000k |
| Total submissions:15926 |
|
accepted:7622 |
Description
Severe Acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, is 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 are many student. Students in the same group Intercommunicate with each of the 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's in a group are a suspect, all members of the group are suspects.
However, they find this it is isn't easy to identify all suspects where a student is recognized as a suspect. Your job is to write a which finds the suspects.
Input
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
The input file contains several cases. Each test case begins with two integers n and m at a line, where n is the number of students, and M is the number of group S. You could assume that 0 < n <= 30000 and 0 <= m <= 500. Every student is numbered by a unique integer between 0 and N1, and initially student 0 are recognized as a suspect in all The cases. This are followed by M/lists of the groups, one line per group. The begins with a integer k by itself representing the number of the group. Following the number of members, there are k integers representing the students in this group. All of the integers in a line are separated by at least one.
A case with n = 0 and M = 0 indicates the "End of" input, and need not is processed.
Output
For each case, output the number of suspects in one line.
Sample Input
4
2 1 2 5 a 2 0 1 2 a 2
2
1 5 5 1 2 3 4 5 1 0 0 0
Sample Output
4
1
1
Source
Asia Kaohsiung 2003
Analysis and Summary:
At the beginning of the complex, but also thought to be kind and check the set = =~, in fact, is the basis of the number of the collection and search ...
Combine each group into one and check the set, note if there are 0, point to 0.
Code:
#include <cstdio> #include <cstring> const int N = 30005;
int n,m,k,f[n],rank[n];
inline void init () {for (int i=0; i<=n; ++i) f[i]=i, rank[i]=1;
int find (int x) {int i, j=x;
while (J!=f[j]) j=f[j];
while (X!=J) {i=f[x]; f[x]=j; x=i;
} return J;
} void Union (int x, int y) {int a=find (x), B=find (y);
if (a==b) return;
if (a==0) {f[b] = A;
Rank[a] + = rank[b];
} else{F[a] = b;
RANK[B] + = Rank[a];
int main () {int u,v;
while (~SCANF ("%d%d", &n,&m) &&n+m) {init ();
int ans=0;
for (int i=0; i<m; ++i) {scanf ("%d", &k);
scanf ("%d", &u);
for (int j=1; j<k; ++j) {scanf ("%d", &v);
Union (U,V);
} printf ("%d\n", rank[0]); } return 0; }
Author: csdn Blog shuangde800