Case of Fake Numbers

来源:互联网 发布:linux sem trywait 编辑:程序博客网 时间:2024/05/16 01:01
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83146#problem/B
Case of Fake Numbers
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 277A

Description

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).

Sample Input

Input
5 51 22 2 32 3 42 4 51 5
Output
0
Input
8 703 1 2 31 12 5 42 6 71 32 7 41 1
Output
2
Input
2 21 20
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个数,从1开始编号,每次对序列的操作是奇树为加一,偶数为减一,问经过有限次操作后能不能构成0,1,2,3,4·······n-1的序列。因为这个序列中的数都是在[0,n-1]区间内的数字,这个序列肯定经过n次数的操作之后会循环

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>#include <cstdlib>#include <limits>#include <queue>#include <stack>using namespace std;#define N 1100#define INF 0xfffffff#define PI acos (-1.0)#define EPS 1e-8const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};int main (){    int a[N], n;    while (cin >> n)    {        int flag = 1;        for (int i=0; i<n; i++)        {            cin >> a[i];            if ( i != a[i])                flag = 0;        }        for (int i=0; i<n && !flag; i++)        {            flag  =1;            for (int j=0; j<n; j++)            {                if (j % 2)                    a[j] = (a[j] + 1) % n;                else                    a[j] = (a[j] + n - 1) % n;                if (a[j] != j)                    flag = 0;            }        }        if (flag)            puts ("Yes");        else            puts ("No");    }    return 0;}


0 0
原创粉丝点击