A. Learning Languages
time limit per test:2 seconds
memory limit per test:256 megabytes


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.

题目链接:http://codeforces.com/problemset/problem/277/A

题目大意:n个员工。m种语言。每一个员工会k种语言,问总共还要学习多少语言,能够让每两个员工能够直接或者间接交流

题目分析:非常明显的并查集问题,注意当k等于0时,该员工必学一门语言。具体见程序凝视

#include <cstdio>
#include <cstring>
int a[105], fa[105]; void UF_set()
{
for(int i = 0; i < 105; i++)
fa[i] = i;
} int Find(int x)
{
return x == fa[x] ? x : fa[x] = Find(fa[x]);
} void Union(int a, int b)
{
int r1 = Find(a);
int r2 = Find(b);
if(r1 != r2)
fa[r2] = r1;
} int main()
{
int n, m, cnt = 0, ans = 0; //ans表示理论上须要学习的人数,cnt表示没人学的
scanf("%d %d", &n, &m);
memset(a, 0, sizeof(a));
UF_set();
for(int i = 0; i < n; i++)
{
int k, fir, next;
scanf("%d", &k);
if(k == 0) //若一门语言都不会。则必要学一门
{
ans++;
continue;
}
scanf("%d", &fir);
a[fir] ++;
for(int i = 1; i < k; i++)
{
scanf("%d", &next);
a[next]++;
Union(fir, next);
}
}
for(int i = 1; i <= m; i++)
{
if(a[i] == 0) //记录没人学的
cnt++;
if(fa[i] == i) //记录集合个数
ans++;
}
//没人学的必定自成一个集合,我们要把它减去,由于既然没一个人学
//它就不须要再被学,若ans等于m说明每两个人之间不能交流。则每人都要学习
//一门语言,否则拿集合个数减去没人学的语言个数再减1,这里减1的含义是
//随意n个集合,n-1条线就能将其连通
printf("%d\n", (cnt == m) ? n : ans - cnt - 1);
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

CodeForces 277A Learning Languages (并检查集合)的更多相关文章

  1. [Codeforces Round #170 Div. 1] 277A Learning Languages

    A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes input standa ...

  2. Codeforces 278C Learning Languages(并查集)

    题意抽象出来就是求联通块的个数吧,然后添加最少边使图联通. 注意所有人都不会任何语言的时候,答案是n而不是n-1. #include<algorithm> #include<iost ...

  3. BZOJ3296: [USACO2011 Open] Learning Languages

    3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 81  Solved: ...

  4. HDU 1272 小希迷宫(并检查集合)

    意甲冠军:被判处无向图无环和连接无处不在 思考:并检查集合,trap 您可能有一个直接输入0 0 并且....合并的时候按某一个方向会爆栈,爆了好几次...下次考虑一下直接递归找祖先吧 #includ ...

  5. BZOJ3296:Learning Languages(简单并查集)

    3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 436  Solved ...

  6. C. Learning Languages 求联通块的个数

    C. Learning Languages 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring&g ...

  7. codeforces 277 A Learning Languages 【DFS 】

    n个人,每个人会一些语言,两个人只要有会一门相同的语言就可以交流,问为了让这n个人都交流,至少还得学多少门语言 先根据n个人之间他们会的语言,建边 再dfs找出有多少个联通块ans,再加ans-1条边 ...

  8. hdu1325 Is It A Tree?并检查集合

    pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ...

  9. URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)

    意甲冠军:n 积分,m 边缘(1 ≤ m < n ≤ 200),问:是否所有的点连接(两个边相交.该 4 点连接). 主题链接:http://acm.timus.ru/problem.aspx? ...

随机推荐

  1. linux上安装Drupal

    linux上安装Drupal 前言:国内用drupal的并不太多,网上资料也很少.要注意的是drupal尽量别使用apt来安装,特别是ubuntu平台的drupal做出了一定的更改,会妨碍后期的学习和 ...

  2. 【Android 应用程序开发】 Fragment 详细说明

    笔者 : 汉书亮 转载请著名出处 : http://blog.csdn.net/shulianghan/article/details/38064191 本博客代码地址 : -- 单一 Fragmen ...

  3. 3g自己主动更新网卡驱动web完架构文档

    几年前写. 看它是否是用得上 1  简单介绍 本文档具体描写叙述了基于ASP.NET平台和IIS服务的T-Mobile自己主动更新系统的实现框架. 本文档主要从技术架构和业务架构两个方面来着手来描写叙 ...

  4. 两个堆叠fragment,上层fragment响应于降低fragment的button点击事件补救措施

    加入onViewCreated的Touch事件监听, 以解决叠在一起的fragment上层响应下层的button点击事件解决方法 @Override public void onViewCreated ...

  5. [MSSQL]最小公约数

    [摘要]一个朋友在展BOM的时候有这种需求,两列字段(数值):A ,B   A=用量,B=底数,组成用量=用量/底数.A/B,若能被整除,显示整除的结果,若不能整除显示分数形式A/B(分数形式要是约分 ...

  6. 当用户登录,经常会有实时的下拉框,例如,输入邮箱,将会@qq.com,@163.com,@sohu.com

    如图所示, 码,如以下:<input id="user_sn" class="loginInput" name="user_sn" t ...

  7. vuejs 相关资料

    官网 http://vuejs.org/ 中文网站 http://cn.vuejs.org/ Vue.js——60分钟快速入门 http://www.cnblogs.com/keepfool/p/56 ...

  8. REPLICAT RORA_1保持 ABENDED状态,无法启动问题处理

    REPLICAT  RORA_1保持 ABENDED状态,无法启动问题 环境: Item Source System Target System Platform Red Hat Enterprise ...

  9. gerrit git使用

    有关git的參考资料 pro git中文版, 最好的git书籍 http://git-scm.com/book/zh 图解git http://marklodato.github.com/visual ...

  10. IE8/IE9无法启用JavaScript怎么办

    在IE8/IE9 中,有些同学在浏览网页时,收到提示:“需要启用 JavaScript …”,并且会发现网页上某些功能不能用了,比如点击网页里的按钮没反应等等.这个是因为浏览器的JavaScript ...