Codeforces Round #170 (Div. 2)---C. Learning Languages(并查集)

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

简单的并查集,注意特判一种情况,所有人一门语言都不会

/*************************************************************************
    > File Name: CF-170-C.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年04月03日 星期五 12时26分27秒
 ************************************************************************/

#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 double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 330;
bool ge[N][N];
int fa[N];

int find(int x)
{
    if (fa[x] == -1)
    {
        return x;
    }
    return fa[x] = find(fa[x]);
}

int main()
{
    int n, m;
    while (~scanf("%d%d", &n, &m))
    {
        memset(ge, 0, sizeof(ge));
        memset(fa, -1, sizeof(fa));
        bool flag = 0;
        for (int i = 1; i <= n; ++i)
        {
            int num, k;
            scanf("%d", &num);
            for (int j = 0; j < num; ++j)
            {
                scanf("%d", &k);
                ge[i][k] = 1;
                flag = 1;
            }
        }
        int ans = n;
        for (int i = 1; i <= n; ++i)
        {
            for (int j = i + 1; j <= n; ++j)
            {
                for (int k = 1; k <= m; ++k)
                {
                    if (ge[i][k] && ge[j][k])
                    {
                        int u = find(i);
                        int v = find(j);
                        if (u != v)
                        {
                            --ans;
                            fa[u] = v;
                        }
                    }
                }
            }
        }
        if (flag)
        {
            printf("%d\n", ans - 1);
        }
        else
        {
            printf("%d\n", ans);
        }
    }
    return 0;
}
时间: 2024-08-11 20:37:27

Codeforces Round #170 (Div. 2)---C. Learning Languages(并查集)的相关文章

Codeforces Round #250 (Div. 1)B(排序+并查集)

B. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The i-th area contains 

Codeforces 278C Learning Languages(并查集) 求连通块

Codeforces 278C Learning Languages(并查集) 求连通块 为什么最后还要getfather 一遍 比如 x 是 y 的父亲 然后你 Union(x,z) 然后 z 变成了 x 父亲 然后 y 的祖先就是错的了 题解 求一个无向图中有几个连通块 sum 特判 一下 如果 每一个人的语言都为 0 则答案为 sum 其他 答案则为 sum - 1 1 #include <bits/stdc++.h> 2 using namespace std ; 3 4 const

CodeForces 277A Learning Languages 并查集

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

Codeforces Round #170 (Div. 1)A

//用并查集记录有几个集合,然后将总的集合数剪一即为所求答案 //注意一下全0就行 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 110 ; int map[maxn][maxn] ; int F[maxn] ; int n , m ; int find(int x) { if(F[x] == x) return F[x] ; re

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un

Codeforces Round #354 (Div. 2) ABCD

Codeforces Round #354 (Div. 2) Problems # Name     A Nicholas and Permutation standard input/output 1 s, 256 MB    x3384 B Pyramid of Glasses standard input/output 1 s, 256 MB    x1462 C Vasya and String standard input/output 1 s, 256 MB    x1393 D T

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #356 (Div. 2) [Codeforces680]

此处有目录↑ Codeforces Round #356(Div. 2):http://codeforces.com/contest/680 A. Bear and Five Cards (贪心) time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output A little bear Limak plays a game. He has

Codeforces Round #177 (Div. 2) 题解

[前言]咦?现在怎么流行打CF了?于是当一帮大爷在执着的打div 1的时候,我偷偷的在刷div 2.至于怎么决定场次嘛,一般我报一个数字A,随便再拉一个人选一个数字B.然后开始做第A^B场.如果觉得机密性不高,来点取模吧.然后今天做的这场少有的AK了.(其实模拟赛只做完了4题,最后1题来不及打了) 等等,话说前面几题不用写题解了?算了,让我难得风光一下啦. [A] A. Polo the Penguin and Segments time limit per test 2 seconds mem