Codeforces 277A Learning Languages (and set)

Source: Internet
Author: User


A. Learning LanguagesTime limit per test:2 secondsmemory limit per test:256 megabytes

the "Bercorp" Company has got N employees. These employees can use m approved official languages for the formal correspondence. The languages is numbered with integers from1 to M. For each employee we have the list of languages, which he knows. This list could is empty, I. E. An employee may know no official languages. But the employees is willing to learn any number of official languages, as long as the company pays their lessons. A Study Course in one language for one employee costs1 berdollar.

Find the minimum sum of money the company needs to spend so as any employee could correspond to any other one (their corre Spondence can indirect, i. e. Other employees can-help out translating).

Input

The first line contains integers n and m (2?≤? N,? m≤?100)-the number of employees and the number of languages.

ThenNLines Follow-each Employee ' s language list. At the beginning of theI-th Line is integer ki (0?≤? k i? ≤? m )-the number of languages theI-th employee knows. Next, theI-th Line contains ki integers- aij (1?≤? a ij≤? m )-the identifiers of Languages theI-th employee knows. It is guaranteed, the identifiers in one list is distinct. Note that a employee may know zero languages.

The numbers in the lines is separated by a single spaces.

Output

Print a single integer-the minimum amount of money "so" in the end every employee could write a letter to ever Y other one (other employees can-help out translating).

Sample Test (s)Input
5 51 22 2 32 3 42 4 51 5
Output
0
Input
8 703 1 2 31 12 5 42 6 71 32 7 41 1
Output
2
Input
2 21 20
Output
1
Note

In the second sample the employee 1 can learn language 2, and employee 8 can learn language 4.

In the third sample employee 2 must learn language 2.


Title Link: Http://codeforces.com/problemset/problem/277/A


The main topic: N Employees, M languages, each staff will be K language, ask the total number of languages to learn, so that every two employees can communicate directly or indirectly


Topic Analysis: Very obvious and check set problem, note that when K equals 0 o'clock, the employee must learn a language, see the program notes in detail


#include <cstdio> #include <cstring>int a[105], fa[105];void Uf_set () {for (int i = 0; i <; i++) Fa[i] = i;} int Find (int x) {return x = = Fa[x]? x:fa[x] = Find (Fa[x]);}    void Union (int a, int b) {int r1 = Find (a);    int r2 = Find (b); if (r1! = r2) FA[R2] = r1;}   int main () {int n, m, cnt = 0, ans = 0;    Ans says the number of people who theoretically need to learn, CNT says no one learns scanf ("%d%d", &n, &m);    memset (A, 0, sizeof (a));    Uf_set ();        for (int i = 0; i < n; i++) {int k, fir, next;        scanf ("%d", &k);            if (k = = 0)//If a language is not, then it is necessary to learn a {ans++;        Continue        } scanf ("%d", &fir);        A[fir] + +;            for (int i = 1; i < K; i++) {scanf ("%d", &next);            a[next]++;        Union (Fir, next);        }} for (int i = 1; I <= m; i++) {if (a[i] = = 0)//record cnt++ that no one learns;    if (fa[i] = = i)//record set number ans++; }//NotThe necessity of human learning becomes a collection, we want to subtract it, because since there is no one to learn//it does not need to be learned, if ans equals m to explain that every two people can not communicate, then everyone must learn//a language, or take the number of sets minus no one to learn the number of languages and then minus 1, here minus 1 meaning is//any n set N-1 line can be connected to printf ("%d\n", (cnt = = m), n:ans-cnt-1);}



Codeforces 277A Learning Languages (and set)

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.