CodeForces 277A Learning Languages (並查集),codeforces277a

來源:互聯網
上載者:User

CodeForces 277A Learning Languages (並查集),codeforces277a

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 are numbered with integers from1 to m. For each employee we have the list of languages, which he knows. This list could be empty, i. e. an employee may know no official languages. But the employees are 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 correspondence can be indirect, i. e. other employees can help out translating).

Input

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

Then n lines follow — each employee's language list. At the beginning of thei-th line is integer ki (0 ≤ ki ≤ m) — the number of languages thei-th employee knows. Next, the i-th line contains ki integers —aij (1 ≤ aij ≤ m) — the identifiers of languages thei-th employee knows. It is guaranteed that all the identifiers in one list are distinct. Note that an employee may know zero languages.

The numbers in the lines are separated by single spaces.

Output

Print a single integer — the minimum amount of money to pay so that in the end every employee could write a letter to every 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.


題目連結:http://codeforces.com/problemset/problem/277/A


題目大意:n個員工,m種語言,每個員工會k種語言,問總共還要學習多少語言,可以讓每兩個員工可以直接或者間接交流


題目分析:很明顯的並查集問題,注意當k等於0時,該員工必學一門語言,詳細見程式注釋


#include <cstdio>#include <cstring>int a[105], fa[105];void UF_set(){    for(int i = 0; i < 105; 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表示理論上需要學習的人數,cnt表示沒人學的    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)      //若一門語言都不會,則必要學一門        {            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)       //記錄沒人學的            cnt++;        if(fa[i] == i)      //記錄集合個數                  ans++;    }    //沒人學的必然自成一個集合,我們要把它減去,因為既然沒一個人學    //它就不需要再被學,若ans等於m說明每兩個人之間不能交流,則每人都要學習    //一門語言,否則拿集合個數減去沒人學的語言個數再減1,這裡減1的含義是    //任意n個集合,n-1條線就能將其連通    printf("%d\n", (cnt == m) ? n : ans - cnt - 1);}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.