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.
Simple and check set, pay attention to a situation, all people a language will not
/************************************************************************* > File name:cf-170-c.cpp > Aut Hor:alex > Mail: [email protected] > Created time:2015 April 03 Friday 12:26 27 seconds ******************************* *****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Const intN = the;BOOLGe[n][n];intFa[n];intFindintx) {if(Fa[x] = =-1) {returnX }returnFA[X] = find (Fa[x]);}intMain () {intN, M; while(~scanf("%d%d", &n, &m)) {memset(GE,0,sizeof(GE));memset(FA,-1,sizeof(FA));BOOLFlag =0; for(inti =1; I <= N; ++i) {intNum, k;scanf("%d", &num); for(intj =0; J < num; ++J) {scanf("%d", &k); GE[I][K] =1; Flag =1; } }intans = n; for(inti =1; I <= N; ++i) { for(intj = i +1; J <= N; ++J) { for(intK =1; K <= m; ++K) {if(Ge[i][k] && ge[j][k]) {intU = Find (i);intv = Find (j);if(U! = V) {--ans; Fa[u] = v; } } } } }if(flag) {printf("%d\n", ans-1); }Else{printf("%d\n", ans); } }return 0;}
Codeforces Round #170 (Div. 2)---C. Learning Languages (and collection)