277A.md (2137B)
1 Source: https://codeforces.com/problemset/problem/277/A 2 3 A. Learning Languages 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 stdin 16 17 output 18 19 stdout 20 21 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. 22 23 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). 24 25 Input 26 27 The first line contains two integers *n* and *m* (2 ≤ *n*, *m* ≤ 100) — the number of employees and the number of languages. 28 29 Then *n* lines follow — each employee's language list. At the beginning of the *i*-th line is integer *k**i* (0 ≤ *k**i* ≤ *m*) — the number of languages the *i*-th employee knows. Next, the *i*-th line contains *k**i* integers — *a**ij* (1 ≤ *a**ij* ≤ *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. 30 31 The numbers in the lines are separated by single spaces. 32 33 Output 34 35 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). 36 37 Examples 38 39 Input 40 41 ``` 42 5 5 43 1 2 44 2 2 3 45 2 3 4 46 2 4 5 47 1 5 48 ``` 49 50 Output 51 52 ``` 53 0 54 ``` 55 56 Input 57 58 ``` 59 8 7 60 0 61 3 1 2 3 62 1 1 63 2 5 4 64 2 6 7 65 1 3 66 2 7 4 67 1 1 68 ``` 69 70 Output 71 72 ``` 73 2 74 ``` 75 76 Input 77 78 ``` 79 2 2 80 1 2 81 0 82 ``` 83 84 Output 85 86 ``` 87 1 88 ``` 89 90 Note 91 92 In the second sample the employee 1 can learn language 2, and employee 8 can learn language 4. 93 94 In the third sample employee 2 must learn language 2.