The "Bercorp" Company has got N employees. These employees can use m approved official languages for the formal correspondence. The languages is numbered with integers from 1 to M. For each employee we have the list of languages, which he knows. This list could is empty, I. E. An employee may know no official languages. But the employees is 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 corre Spondence can indirect, i. e. Other employees can-help out translating).
Input
The first line contains 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 an 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, the identifiers in one list is distinct. Note that a employee may know zero languages.
The numbers in the lines is separated by a single spaces.
Output
Print a single integer-the minimum amount of money "so" in the end every employee could write a letter to ever Y other one (other employees can-help out translating).
Sample Test (s)
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.
Main topic
There are n people, M languages. Give everyone the language (or maybe not), ask at least a few people to learn the language, can make people can communicate with each other.
Thinking of solving problems
Use and look up the set and find the same language to put them together. Finally found that par[i] = i in the case there are two. One is not a language, and there is language. Find that if the language of the collection <=1, how many languages do not need to have a few languages. Direct output is available.
If the language collection >1, you need to add a set of languages and then-1.
(In other words, it is possible to use a set of languages that are not language--1, but all of them are not language collections.)
Code
#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;Const intMAXN = the;intMEM[MAXN];//mem[i] Indicates which person I was last seen inintPAR[MAXN];intF[MAXN];intN,m;int_find (intx) {if(x = = Par[x])returnXreturnPAR[X] = _find (Par[x]);}void_unite (intAintb) {a = _find (a); b = _find (b);if(A! = b) Par[a] = b;}intMain () {scanf("%d%d", &n,&m); for(inti =1; I <= N; i + +) par[i] = i; for(inti =1; I <= N; i + +) {intQscanf("%d", &q);if(q = =0) F[i] =1; while(q--) {intAscanf("%d", &a);if(Mem[a]! =0) {_unite (mem[a],i);Continue;} Mem[a] = i; } }intCNT =0; for(inti =1; I <= N; i + +)if(Par[i] = = i && f[i]) cnt++;int_cnt =0; for(inti =1; I <= N; i + +)if(Par[i] = = i &&!f[i]) _cnt++;if(_cnt >1) cnt + = (_cnt-1);printf("%d\n", CNT);return 0;}
Codeforces 277A Learning Languages and check set