A - Learning Languages CodeForces - 277A(并查集)

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 5
1 2
2 2 3
2 3 4
2 4 5
1 5
Output
0
Input
8 7
0
3 1 2 3
1 1
2 5 4
2 6 7
1 3
2 7 4
1 1
Output
2
Input
2 2
1 2
0
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.


大意就是让这些人互相沟通,最少需要学语言的次数。

用并查集将使用过的语言给连接起来,那么最后剩多少个祖先,每个都代表没能成功与其他部分交流的。注意如果会0门语言,那必须学一次,如果该语言没使用过,则不作为咱的考虑对象。

代码

#include"iostream"
#include"algorithm"
#include"cstring"
#include"set"
using namespace std;
int pre[150];

void init()
{
	for(int i = 0;i < 101;i ++)
	{
		pre[i]=i;
	}
}
int find(int x)
{
	return x==pre[x] ? x:pre[x]=find(pre[x]);
}
void join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx != fy)
	{
		pre[fx]=fy;
	}
}

int main()
{
	int n,m;
	while(cin >> n >> m)
	{
		init();
		int vst[105]={0};
		int sum =0 ;
		for(int i = 0;i < n;i ++)
		{
			int t;
			cin >> t;
			int a[105];
			if(t==0)
			{
				sum ++;
				continue;
			}
			cin >> a[0];
			vst[a[0]]++;
			for(int  j = 1;j < t;j ++)
			{
				 cin >>a[j];
				 vst[a[j]]++;
				 join(a[j-1],a[j]);
			}
			 
		}
		int pl = 0;
	/*	cout<<"-------------"<<endl;
		for(int i = 1;i <= m;i ++)
		{
			cout<<pre[i]<<" "<<i<<endl;
		}
		cout<<"-------------"<<endl;*/
		for(int i = 1;i <= m;i ++)
		{
			if(vst[i])
			{
				if(pre[i]==i)
				{
					pl++;
				}
			}
		}
	//	cout<<pl<<endl;
		pl=max(0,pl-1);
		cout<<sum+pl<<endl;
		
	}
	return 0;
} 

发布了58 篇原创文章 · 获赞 20 · 访问量 5406

猜你喜欢

转载自blog.csdn.net/qq_41658124/article/details/80989119
今日推荐