题干:
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).
![](https://i-blog.csdnimg.cn/blog_migrate/ab28e452fad6005e482bb9f93600c5e2.png)
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.
题意:
给出总共多少个员工和多少种语言,然后每个员工给出对应数量的语言的种类。询问需要再学习多少种语言才能使员工们能够沟通。
思路:
使员工们能互相沟通,换言之就是最少有一条通路,只需要找出员工连通块的个数-1即可。俩个员工彼此掌握的语言互相匹配,如果中间有相同的那么就可以连通。
代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=105;
const int mod=998244353;
set<int> s[N];
ll ans;
int f[N];
//并查集模板
int getf(int x){
if(f[x]==x) return x;
else return f[x]=getf(f[x]);
}
void merge(int x,int y){
f[getf(x)]=getf(y);
}
inline int iread(){
int ans=0,sign=1;
char ch = getchar();
while(ch<'0'||ch>'9'){
if(ch==' '){
sign=-1;
}
ch = getchar();
}
while(ch>='0'&&ch<='9'){
ans = ans*10 + ch-'0';
ch = getchar();
}
return ans*sign;
}
int n,m;
int main(){
n=iread(),m=iread();
for(int i=1;i<=n;i++){
f[i]=i;
}
int flag=0;
//存储每个员工掌握的语言
for(int i=1;i<=n;i++){
int num;
num=iread();
if(num>0) flag=1;
for(int j=1;j<=num;j++){
int x;
x=iread();
s[i].insert(x);
}
}//判断是否都不连通
if(flag==0){
cout<<n;
return 0;
}
// for(int i=1;i<=n;i++){
// cout<<i<<":";
// for(set<int>::iterator item=s[i].begin();item!=s[i].end();item++){
// cout<<"item:"<<*item<<endl;
// }
// }
//2个循环将员工每队都进行比较
for(int i=1;i<n;i++){
for(int j=i+1;j<=n;j++){
//遍历俩个员工掌握的语言
for(set<int>::iterator item=s[i].begin();item!=s[i].end();item++){
for(set<int>::iterator item2=s[j].begin();item2!=s[j].end();item2++){
if(*item==*item2){
merge(i,j);
break;
}
}
}
}
}
//判断连通块数量
for(int i=1;i<=n;i++){
if(f[i]==i){
ans++;
}
}
cout<<ans-1;
return 0;
}