題目:
The "BerCorp" company has got n employees. These employees can use mapproved 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).
Example
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
Hint
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種語(yǔ)言,輸入每名員工會(huì)多少種語(yǔ)言以及會(huì)的語(yǔ)言的編號(hào)(有可能有員工一門(mén)都不會(huì))。現(xiàn)在公司可以給員工培訓(xùn)語(yǔ)言,但培訓(xùn)一個(gè)就要花1個(gè)幣。問(wèn)公司最少花多少錢(qián)可以保證這n名員工可以直接或間接(可以通過(guò)別人翻譯,但翻譯的人需懂那兩個(gè)人的語(yǔ)言)進(jìn)行交流。
思路:并查集。
參考代碼:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
const int N = 100+5;
map<int, int> v[N];//對(duì)應(yīng)語(yǔ)言, 該員工是否學(xué)習(xí)了這門(mén)語(yǔ)言;
int par[N];
bool flag = true;//檢查是否所有人都不會(huì)任何一種語(yǔ)言, 如果是那么就給所有人教同一門(mén)語(yǔ)言;//true為是;
void init() {
for (int i = 0;i < N;++i) par[i] = i;
}
void input(const int n) {
int num;//每個(gè)人會(huì)的語(yǔ)言數(shù);
int lan;//每個(gè)人會(huì)的語(yǔ)言的編號(hào);
for (int i = 1;i <= n;++i) {
cin >> num;
if (num > 0) flag = false;
for (int j = 1;j <= num;++j) {
cin >> lan;
v[i][lan] = 1;//表明第i名員工會(huì)lan這門(mén)語(yǔ)言;
}
}
}
//并查集;
int finds(int x) {
if (x == par[x]) return x;
else return par[x] = finds(par[x]);
}
void unite(int x, int y) {
int x1 = finds(x);
int y1 = finds(y);
if (x1 != y1) {
par[y1] = x1;
}
}
int judge(const int n, const int m) {
if (flag == true) return n;
for (int k = 1;k <= m;++k) {
for (int i = 1;i <= n;++i) {
for (int j = 1;j <= n;++j) {
if (v[i][k] == 1 && v[j][k] == 1) {//代表他們兩個(gè)都會(huì)k這門(mén)語(yǔ)言;
unite(i, j);
}
}
}
}
//for (int i = 1;i <= n;++i) {
// cout << par[i] << " ";//可以直接或間接交流的人用并查集并為一個(gè)集合;
//}
//cout << endl;
int ans = 0;
for (int i = 1;i <= n;++i) {
if (par[i] == i) {
++ans;
}
}
//cout << ans << endl;
//如果有合并情況, 兩個(gè)集合中任意一個(gè)集合中的任意一個(gè)人學(xué)會(huì)另外一個(gè)集合中任意一個(gè)人會(huì)的語(yǔ)言即可;
return ans - 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
init();
input(n);
int ans = judge(n, m);
cout << ans << endl;
return 0;
}