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

题目大意

有n个人,m种语言.给出每个人会的语言(也可能一种都不会),问最少让几个人学语言,可以使得大家可以互相沟通.

解题思路

利用并查集,发现会同一种语言的就把他们放到一起.最后发现par[i] = i的情况有两种.一种是一种语言都不会的,还有就是会语言的.发现如果会语言的集合<=1时,有多少不会语言的就需要有几个学语言的.直接输出即可.

如果会语言的集合>1时,需要再加上会语言的集合再-1.

(换句话说,可以用不会语言的集合+会语言的集合-1,但是全都是不会语言的集合的时候需要特判)

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 110;
int mem[maxn];//mem[i]表示i语言上次出现在哪个人
int par[maxn];
int f[maxn];
int n,m;
int _find(int x)
{
    if(x == par[x]) return x;
    return par[x] = _find(par[x]);
}
void _unite(int a,int b)
{
    a = _find(a);
    b = _find(b);
    if(a != b) par[a] = b;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i = 1 ; i <= n ; i ++) par[i] = i;
    for(int i = 1 ; i <= n ; i ++) {
        int q;
        scanf("%d",&q);
        if(q == 0) f[i] = 1;
        while(q--) {
            int a;
            scanf("%d",&a);
            if(mem[a] != 0) {_unite(mem[a],i);continue;}
            mem[a] = i;
        }
    }
    int cnt = 0;
    for(int i = 1; i <= n ; i ++) if(par[i] == i && f[i]) cnt++;
    int _cnt = 0;
    for(int i = 1 ; i <= n ; i ++) if(par[i] == i && !f[i]) _cnt++;
    if(_cnt > 1) cnt += (_cnt-1);
    printf("%d\n",cnt);
    return 0;
}
时间: 2024-11-05 18:46:35

CodeForces 277A Learning Languages 并查集的相关文章

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

codeforces 468B two set(并查集)

codeforces 468B two set(并查集)n+1 表示 B 组 n+2 表示 A 组 按照 这题的做法 应该是 如果 满足 num[ i ] a-num[ i ] 则他们同一组 但不一定 就一定是 都是 A 组 也可能都是 B 组 然而如果不满足这个条件的话,就直接加入 B组 然后如果满足 num[ i ] b-num[ i ] 则加入同一组 然后不满足就 加入 A 组 1 #include <bits/stdc++.h> 2 using namespace std ; 3 4

Codeforces 292D Connected Components (并查集)

Codeforces 292D Connected Components (并查集) 题意 给出一张无向图,每次询问删去第Li--Ri 条边 求此时有多少个连通块 题解 求出一个前缀 Li 表示 加入前 i 条边时图的连通状况 以及一个后缀 Ri 表示 加入后 i 条边时图的连通状况 对于每个询问 删除 s--t 条边 只要将 L s-1 和 R t+1 合并 一下 就行了 合并 其实 就是讲 s-1 和 t+1 对应的 f[ i ] Union 一下就行了 为什么 这就相当于把前缀 i 和 后

Codeforces Gym 100463E Spies 并查集

Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Description In the aftermath of Canada’s annexation of Pittsburgh tensions have been pretty high between Canada and the US. You have personally been hired

Codeforces 650C Table Compression (并查集)

题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! 1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 #include <algorithm> 5 #include <cmath> 6 #include <cstdlib> 7 #include <bits/stdc

CodeForces 731C C - Socks 并查集

Description Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes. Ten minutes before her leave she real

CodeForces 722C Destroying Array (并查集)

题意:给定 n 个数,然后每次破坏一个位置的数,那么剩下的连通块的和最大是多少. 析:用并查集来做,从后往前推,一开始什么也没有,如果破坏一个,那么我们就加上一个,然后判断它左右两侧是不是存在,如果存在,那么就合并起来, 然后不断最大值,因为这个最大值肯定是不递减,所以我们一直更新就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <s

CodeForces 566D Restructuring Company (并查集+链表)

题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并查集来做,但是如果单纯的用并查集,肯定是要超时的,所以要用链表,如果合并了,就把链表指向, 这样就搞定了这个题. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #i