题干:

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.

题目大意:

公司一共有N个人,一共官方有M种语言,其中已知每个人会的语言,如果两个人会同一种语言,那么两个人就可以相互交流,如果A会语言1,2,B会语言2,3 C会语言3 4,那么B可以给C翻译A说的话,说以这时候根据这个特性,那么ABC三个人就可以相互交流了。

公司为了让这N个人都能够相互交流,可以使得一些人学会一门语言,对应花费为1,问最少花费多少能够达到目的。

解题报告:

   刚开始想着枚举每一个人,,然后看其他人有没有一种语言可以与他配对,,,如果都不能配对就ans+1。。。。其实是不对的,,因为你这样跑出来还是有可能不会使图是连通的。。。题意抽象出来就是求联通块的个数吧,然后添加最少边使图联通啊mmp。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int a[102][102][102];
int f[102];
set<int> ss[102];
int getf(int v) {
	return v == f[v] ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {
	int t1 = getf(u);
	int t2 = getf(v);
	if(t1 != t2) f[t2]=t1;
}
int main()
{
	int n,m;
	int ans=0,fl=0;
	cin>>n>>m;
	for(int i = 1; i<=n; i++) f[i]=i;
	for(int i = 1,num,tmp; i<=n; i++) {
		scanf("%d",&num);
		if(num>0) fl=1;
		while(num--) {
			scanf("%d",&tmp);
			ss[i].insert(tmp);
		}
	}
	if(fl==0) {
		printf("%d\n",n);return 0 ;
	}
	for(int k = 1; k<=m; k++) {
		for(int i = 1; i<=n; i++) {
			for(int j = 1; j<=n; j++) {
				if(ss[i].find(k) != ss[i].end() && ss[j].find(k) != ss[j].end()) merge(i,j);//a[i][j][k]=a[j][i][k]=1; 
			}
		}
	}
	for(int i = 1; i<=n; i++) 
		if(f[i] == i) ans++;
//	for(int i = 1; i<=n; i++) {
//		int flag = 0;
//		for(int j = 1; j<=n; j++) {
//			if(i==j) continue;
//			for(int k = 1; k<=m; k++) {
//				if(a[i][j][k] == 1) {
//					flag=1;break;
//				}
//			}
//			if(flag==1) break;
//		}
//		if(flag==0) ans++;
//	}
	printf("%d\n",ans-1);
	
//	for(int k = 1; k<=n; k++) {//遍历每一个人 
////		int up = ss[i].size();
//		int flag=0;
//		for(set<int>::iterator it = ss[k].begin(); it != ss[k].end(); ++it) {//遍历这个人会的每一种语言 
//			for(int i = 1; i<=n; i++) {
//				if(i==k) continue;
//				if(ss[i].find(*it) != ss[i].end()) {
//					flag=1;break;
//				}
//			}
//		} 
//		if(flag == 0) ans++;
//	}
//	printf("%d\n",ans/2);
	return 0 ;
 }

总结:

   遇到问题还是要想着先建模,,不要着急敲代码,,预处理的模型建好了思路才清晰、、