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. JSF教程(9)——生命周期之Process Validations Phase

    在这个过程其中JSF的实现者使用processValidators方法处理全部在tree中的组件中注冊的验证器.验证的过程就是通过每一个组件已有的规则对其已经保存的值进行校验,同一时候也对输入的值进行 ...

  2. tcpdump VS tshark用法(转)

    Tcpdump是网络协议分析的基本工具.tshark是大名鼎鼎的开源网络协议分析工具wireshark (原名叫ethereal)的命令行版本,wireshark可对多达千余种网络协议进行解码分析.W ...

  3. Node.js 博客实例(五)编辑与删除功能

    原教程 https://github.com/nswbmw/N-blog/wiki/_pages的第五章,因为版本号等的原因,在原教程基础上稍加修改就可以实现. 如今给博客加入编辑文章与删除文章的功能 ...

  4. centos7关闭防火墙(转)

    直接命令:service firewalld stop 1. Disable Firewalld Service. [root@rhel-centos7-tejas-barot-linux ~]# s ...

  5. 让你的字ScrollView、ListView充分伸展

    android默认ScrollView.ListView在最顶部的下拉上拉时或底部,未与反弹效应,很僵,让你无法继续拖累,不比iOS至于能否反弹.个人觉得,iOS互动还是略胜一筹.因此,我们已经走在A ...

  6. WPF技术触屏上的应用系列(四): 3D效果图片播放器(图片立体轮放、图片立体轮播、图片倒影立体滚动)效果实现

    原文:WPF技术触屏上的应用系列(四): 3D效果图片播放器(图片立体轮放.图片立体轮播.图片倒影立体滚动)效果实现 去年某客户单位要做个大屏触屏应用,要对档案资源进行展示之用.客户端是Window7 ...

  7. Myeclipse它显示了一个目录的结构,而不是包

    今天Myeclipse新project,编写代码,查找workspace空间展示project在包装和class所有平行结构,看的很不顺,有两个原因,第一,您可能无法切换到Package worksp ...

  8. Post数据到 https异常:基础连接已经关闭: 连接被意外关闭 解决办法

    POST数据到HTTPS站点的时候需要设置ServicePointManager类的ServerCertificateValidationCallback属性,并且在POST到https://pass ...

  9. pyspark简要原则

    概要 这是一个看前一段时间spark的python支持的时,有点简单的后pyspark内python代码,我们把一个一般流程.虽然几乎没有python,但基本上能看懂pyspark它是如何使不同的虚拟 ...

  10. ios至于理解锚

    锚点ios出现在少数地方,多数用在动画. 今天看了一部电影,以上所有关于锚,两年前锚这个概念看cocos2d当被接触的基本概念,当时我没怎么看,今天看了,刚刚好学习. 阅读blog,它是关于锚,像: ...