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. swift-辞典NSDictionary定义,变化的关键,删/加入关键

    // Playground - noun: a place where people can play import UIKit //--------------------------------- ...

  2. poj2752 Seek the Name, Seek the Fame(next数组的运用)

    题目链接:id=2752" style="color:rgb(202,0,0); text-decoration:none; font-family:Arial; font-siz ...

  3. 在JAVA中使用LUA脚本记,javaj调用lua脚本的函数(转)

    最近在做一些奇怪的东西,需要Java应用能够接受用户提交的脚本并执行,网络部分我选择了NanoHTTPD提供基本的HTTP服务器支持,并在Java能承载的许多脚本语言中选择了很久,比如Rhino,Jy ...

  4. oracle表空间查询维护命令大全之三(暂时表空间)史上最全

    --UNDO表空间汇总 --查看全部的表空间名字 SELECT NAME FROM V$TABLESPACE; --创建新的UNDO表空间,并设置自己主动扩展參数; CREATE UNDO TABLE ...

  5. android学习经常使用的数据文件夹

    android工程实践 1.仿360一键清理实现(一) "一键清理"是一个桌面图标,点击图标后,显示一个视图.进行清理动画.之后显示清理了几个进程,释放了多少M内存.点击" ...

  6. 九度OJ 1177 查找 (模拟)

    题目1177:查找 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5659 解决:1667 题目描写叙述: 读入一组字符串(待操作的),再读入一个int n记录记下来有几条命令,总共同拥有 ...

  7. Nagios显示器mysql定从库: libmysqlclient.so.18: cannot open shared object file: No such

    做mysql的slave时间监控,必须check_mysql文字,check当误差: error while loading shared libraries: libmysqlclient.so.1 ...

  8. 在AcGIS随着大数据的生成DEM

    在ArcGIS产生DEM时间.数据来源是经常有的高程点.轮廓线,该高程点.等高线密集,可能有几千万.甚至亿高程点.轮廓线. 如果您使用这些矢量数据生成TIN.不能实现的,由于生成TIN时,支持的最大结 ...

  9. robot framework 使用三:他们主动浏览器的兼容性

    robot framework 浏览器兼容性测试 上图中黄色圈的地方默认什么都不写,是firefox浏览器.写上ie就是ie浏览器了 firefox最新版本号即可,ie须要设置: 1. IE选项设置的 ...

  10. 2014阿里巴巴web前实习生项目分析(1)

    以下简化CSS代码: div.container{ width:500px; background-image:url(/img/sprite.png); background-repeat:no-r ...