一、题目:

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 1

Inputcopy Outputcopy
5 5
1 2
2 2 3
2 3 4
2 4 5
1 5
0

Sample 2

Inputcopy Outputcopy
8 7
0
3 1 2 3
1 1
2 5 4
2 6 7
1 3
2 7 4
1 1
2

Sample 3

Inputcopy Outputcopy
2 2
1 2
0
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种语言,如果A会语言1,2,B会语言2,3 C会语言3 4,则ABC三个人就可以相互交流了,即是一个集合。

公司为了让这N个人都能够相互交流,可以使得一些人学会一门语言,对应花费为1,问最少花费多少?

二、思路:

能够相互说话的元素合并成一个集合,则该集合的每个元素的根节点相同,不同集合的集合根节点不同,若有两个集合,只需要向其中一个集合加入另一结合中存在的语言即可合并,则x个集合,就花费x-1元,但x==n时,此时有n个集合,但每个人至少的要学一种语言,则花费n元。

三、代码:

#include<iostream>
using namespace std;
int parent[205];
int find(int x) {if (parent[x] == x) return x;return parent[x] = find(parent[x]);//状态压缩
}
void unit(int r, int y) {int r1 = find(r);int y1 = find(y);parent[y1] = r1;
}
int main()
{int n, m, l, k;int flag = 0;cin >> n >> m;int ans = 0;for (int i = 1; i <= n + m; i++) {parent[i] = i;//初始化}for (int i = 1; i <= n; i++) {cin >> l;if (l == 0) continue;elsefor (int j = 1; j <= l; j++) {cin >> k;unit(i, k + n);//一人为根节点flag = 1;}}for (int i = 1; i <= n; i++) {if (parent[i] == i)ans++;//parent[i] == i,遍历根节点数,表示集合数。}if (!flag)cout << n;//特判每人至少学一个语言。else cout << ans - 1;
}

B - Learning Languages相关推荐

  1. 刷题记录:牛客NC24608[USACO 2011 Ope S]Learning Languages

    传送门:牛客 题目描述: [USACO11OPEN]Learning Languages S 农夫约翰的 N ( 2 < = N < = 10 , 000 ) N(2<=N<= ...

  2. A - Learning Languages

    A - Learning Languages 在 "BerCorp" 公司,有 n 名雇员.这些雇员可以使用 m 种核准的正式语言,用于正式的通信.这些语言以整数编号,介于 1 到 ...

  3. 【CodeForces - 278C 】Learning Languages(并查集,思维)

    题干: The "BerCorp" company has got n employees. These employees can use m approved official ...

  4. Istio微服务平台集成实践

    前言 Istio发布1.0版本后,其服务发现和路由规则功能已基本具备production能力,我们也开始了Istio和公司内部微服务平台的集成工作,打算以Istio为基础打造一个微服务管控中心,在这里 ...

  5. 英语国家的学生学语法么?_纪念国家语法日

    英语国家的学生学语法么? by Pete Considine 皮特·康斯丁(Pete Considine) 纪念国家语法日 (In Honor of National Grammar Day) -或作 ...

  6. 基于tcp的网络程序_【CVPR 2020 Tutorial】基于神经网络的符号化视觉推理和程序合成(2)...

    往期回顾:https://zhuanlan.zhihu.com/p/165665242 第三讲:Neuro-Symbolic Visual Concept Learning 主讲:Jiayuan Ma ...

  7. sql server高可用_SQL Server 2019常规可用性和安装概述

    sql server高可用 介绍 (Introduction) On November 4th, 2019, during the Ignite conference at Orlando, Micr ...

  8. [渝粤教育] 深圳信息职业技术学院 《新理念英语》English For You 参考 资料

    教育 -<新理念英语>English For You-章节资料考试资料-深圳信息职业技术学院[] Which is true about the introduction? 1.[多选题] ...

  9. 语法入门*算法入门题单

    作者:王清楚 链接:https://ac.nowcoder.com/discuss/817596?type=101&order=0&pos=1&page=4&chann ...

最新文章

  1. mysql1040_mysql Error 1040 too many connection解决办法
  2. java同步异步调用_详解java 三种调用机制(同步、回调、异步)
  3. Linux YUM扩展NFS共享存储服务(配置图解)
  4. Java NIO:浅析I/O模型
  5. Java并发编程实战~final
  6. input hidden用法
  7. 1100名达摩院“扫地僧”加持,阿里云的下一个十年
  8. visio 2003 for enterprice architects 不需要安装VS2005,直接安装的方法
  9. (五)ThinkPHP实践之Session驱动-TTLSA
  10. chorme浏览器广告终结者视频白频解决办法
  11. Mac教程——创建txt文件、设置新建txt的快捷键方法
  12. 卫星星历TLE格式说明
  13. 暑期作息时间表模板_暑假作息时间表
  14. 怎么把mov视频转换成mp4格式
  15. 智慧课堂实时监测系统
  16. Android串口开发
  17. 树莓派上编写串口助手软件
  18. html5猜大王游戏,5月8日维护:新副本金兜洞兕大王部分测试
  19. 三次样条曲线CubicSpline
  20. CC2530内部结构图

热门文章

  1. GNU Screen的使用方法
  2. fiddler编程猫死活hook不到的解决办法之一
  3. 荣耀20手机不升级鸿蒙经验
  4. OpenStack——nove组件部署
  5. Lacking counting POJ.NO 2386
  6. ps4移植android游戏,安卓玩PS4游戏 remote play安装教程
  7. 野路子玩Qt,第十集,八音盒
  8. iOS开发中的零碎知识点笔记 韩俊强的博客
  9. 字符串正则替换、点替换横杠
  10. 电影暗杀了一只巨可爱的猫[转帖]