codeforce C. Learning Languages(并查集)

来源:互联网 发布:复杂网络 计算机 编辑:程序博客网 时间:2024/05/22 08:18

题目链接:点击打开链接

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 from 1 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 costs 1 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 the i-th line is integer ki (0 ≤ ki ≤ m) — the number of languages the i-th employee knows. Next, the i-th line contains ki integers — aij (1 ≤ aij ≤ m) — the identifiers of languages the i-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).

Examples
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.

题目大意:公司有n个人,有m种语言,每个人可会多种语言,也可以一种也不会.要求员工去学习语言,每种语言每门课程花费1美元,问最少花多少钱能上员工间互相交流,可以一个人给另外一个当翻译.
基本思路:很明显就是并查集求联通分量的个数,只要在不同联通分量中的人学习另一个联通分量中的一种语言就可以联通了.要想把两个人直接连起来,要求他们有共同的语言,我在建图时用的结构体,是先根据语言的种类划分的,将用同种语言的人在一个数组中,然后在暴力建图.可是这样做有一个数据过不了,原因就是题目中可能都不会说话,这样的话要特殊考虑,就要学习n次了.所以在这里wa了两次.
#include <iostream>#include<cstring>#include<cstdio>using namespace std;struct node{    int a[105];    int len;} q[105];int mp[105][105];int arr[105];int f(int x){    while(x!=arr[x])    {        x=arr[x];    }    return x;}int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        memset(q,0,sizeof(q));        memset(mp,0,sizeof(mp));        int k,x;        int ans=0;        for(int i=1; i<=n; i++)        {            arr[i]=i;            scanf("%d",&k);            if(k==0)ans++;            for(int j=0; j<k; j++)            {                scanf("%d",&x);                q[x].a[q[x].len++]=i;            }        }        for(int i=1; i<=m; i++)        {            for(int j=0; j<q[i].len; j++)            {                for(int k=j+1; k<q[i].len; k++)                {                    mp[q[i].a[j]][q[i].a[k]]=1;                    mp[q[i].a[k]][q[i].a[j]]=1;                }            }        }        for(int i=1; i<=n; i++)        {            for(int j=1; j<=n; j++)            {                if(mp[i][j]==1&&f(i)!=f(j))                {                    arr[f(i)]=f(j);                }            }        }        int sum=0;        for(int i=1; i<=n; i++)        {            if(arr[i]==i)sum++;        }        if(ans==n)cout<<ans<<endl;        else            cout<<sum-1<<endl;    }    return 0;}



0 0