【CodeForces - 278C 】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).

Examples

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种语言,其中已知每个人会的语言,如果两个人会同一种语言,那么两个人就可以相互交流,如果A会语言1,2,B会语言2,3 C会语言3 4,那么B可以给C翻译A说的话,说以这时候根据这个特性,那么ABC三个人就可以相互交流了。

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

解题报告:

   刚开始想着枚举每一个人,,然后看其他人有没有一种语言可以与他配对,,,如果都不能配对就ans+1。。。。其实是不对的,,因为你这样跑出来还是有可能不会使图是连通的。。。题意抽象出来就是求联通块的个数吧,然后添加最少边使图联通啊mmp。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int a[102][102][102];
int f[102];
set<int> ss[102];
int getf(int v) {return v == f[v] ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {int t1 = getf(u);int t2 = getf(v);if(t1 != t2) f[t2]=t1;
}
int main()
{int n,m;int ans=0,fl=0;cin>>n>>m;for(int i = 1; i<=n; i++) f[i]=i;for(int i = 1,num,tmp; i<=n; i++) {scanf("%d",&num);if(num>0) fl=1;while(num--) {scanf("%d",&tmp);ss[i].insert(tmp);}}if(fl==0) {printf("%d\n",n);return 0 ;}for(int k = 1; k<=m; k++) {for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {if(ss[i].find(k) != ss[i].end() && ss[j].find(k) != ss[j].end()) merge(i,j);//a[i][j][k]=a[j][i][k]=1; }}}for(int i = 1; i<=n; i++) if(f[i] == i) ans++;
//	for(int i = 1; i<=n; i++) {
//		int flag = 0;
//		for(int j = 1; j<=n; j++) {
//			if(i==j) continue;
//			for(int k = 1; k<=m; k++) {
//				if(a[i][j][k] == 1) {
//					flag=1;break;
//				}
//			}
//			if(flag==1) break;
//		}
//		if(flag==0) ans++;
//	}printf("%d\n",ans-1);//	for(int k = 1; k<=n; k++) {//遍历每一个人 int up = ss[i].size();
//		int flag=0;
//		for(set<int>::iterator it = ss[k].begin(); it != ss[k].end(); ++it) {//遍历这个人会的每一种语言 
//			for(int i = 1; i<=n; i++) {
//				if(i==k) continue;
//				if(ss[i].find(*it) != ss[i].end()) {
//					flag=1;break;
//				}
//			}
//		} 
//		if(flag == 0) ans++;
//	}
//	printf("%d\n",ans/2);return 0 ;}

总结:

   遇到问题还是要想着先建模,,不要着急敲代码,,预处理的模型建好了思路才清晰、、

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441151.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java短除法获取二进制_Java十四天零基础入门-Java的数据类型介绍

不闲聊&#xff01;&#xff01;&#xff01;不扯淡&#xff01;&#xff01;&#xff01;小UP只分享Java相关的资源干货本章节目标&#xff1a;理解数据类型的作用。Java中包括哪些数据类型&#xff1f;常见的八种基本数据类型都有哪些&#xff1f;会用八种基本数据类型声明变…

【CodeForces - 151C】Win or Freeze (博弈,数学,唯一素数分解)

题干&#xff1a; You cant possibly imagine how cold our friends are this winter in Nvodsk! Two of them play the following game to warm up: initially a piece of paper has an integer q. During a move a player should write any integer number that is a non-tr…

vs怎么把textbox输入的实数放置变量里_方程的计算机处理96(3)_C++vs

计算机语言运用--数值计算9-方程的计算机处理96(3)_Cvs计算机&#xff1a;电子线路组成的计算机器。人与计算机则是通过计算机语言-符号系统说给计算机听而交流。计算机语言有低级语言-机器语言、汇编、高级语言-C/C/C#/VB/PASCAL/LISP/JAVA/PYTHON/……成百上千种之多。作为一…

【CodeForces - 151D】Quantity of Strings (字符串问题,思维推导,有坑)

题干&#xff1a; Just in case somebody missed it: this winter is totally cold in Nvodsk! It is so cold that one gets funny thoughts. For example, lets say there are strings with the length exactly n, based on the alphabet of size m. Any its substring with…

2008r装mysql_mysql5.7.17在win2008R2的64位系统安装与配置实例

安装MySql操作系统&#xff1a;Windows Server 2008 R2 StandardMySql版本&#xff1a;mysql-5.7.17-winx64第一步&#xff1a;解压mysql-5.7.17-winx64.zip到指定位置第二步&#xff1a;安装文件根目录下添加data文件夹&#xff0c;将my-default.ini重命名为my.ini第三步&…

【POJ - 2377】Bad Cowtractors (最大生成树,并查集)

题干&#xff1a; Bessie has been hired to build a cheap internet network among Farmer Johns N (2 < N < 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 < M < 20,000) possible connection route…

csv转为utf8编码_读取UTF8编码的CSV并转换为UTF-16

我正在读取具有UTF8编码的CSV文件&#xff1a;ifile open(fname, "r")for row in csv.reader(ifile):name row[0]print repr(row[0])这很好用&#xff0c;并打印出我希望它打印出来的东西; UTF8编码 str &#xff1a;> \xc3\x81lvaro Salazar> \xc3\x89lodie…

【 CodeForces - 799A 】Carrot Cakes(模拟,细节,有坑)

题干&#xff1a; In some game by Playrix it takes t minutes for an oven to bake k carrot cakes, all cakes are ready at the same moment t minutes after they started baking. Arkady needs at least n cakes to complete a task, but he currently dont have any. H…

mysql 递归实现树形_Mysql实现树形递归查询

最近在做项目迁移&#xff0c;Oracle版本的迁到Mysql版本&#xff0c;遇到有些oracle的函数&#xff0c;mysql并没有&#xff0c;所以就只好想自定义函数或者找到替换函数的方法进行改造。Oracle递归查询oracle实现递归查询的话&#xff0c;就可以使用start with ... connect b…

【CH - 1401】 兔子与兔子(字符串哈希)

题干&#xff1a; 描述 很久很久以前&#xff0c;森林里住着一群兔子。有一天&#xff0c;兔子们想要研究自己的 DNA 序列。我们首先选取一个好长好长的 DNA 序列&#xff08;小兔子是外星生物&#xff0c;DNA 序列可能包含 26 个小写英文字母&#xff09;&#xff0c;然后我…

postmain请求中午乱码_完美解决Get和Post请求中文乱码的问题

对于Post请求&#xff0c;只需在Servlet或者jsp中写入如下代码就可以把解决从表单中传入的中文乱码问题request.setCharacterEncoding("utf-8");而对于Get请求&#xff0c;因为请求参数会被附加到地址栏的URL之后&#xff0c;所以不能用上面的处理方法。应该这样&…

【HDU - 5187】zhx's contest (快速幂+ 快速乘,模板)

题干&#xff1a; 2018百度之星复赛晋级名单出炉&#xff08;增加20%晋级名额&#xff09;~ zhxs contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 3779 Accepted Submission(s): 1226 Problem Desc…

python批量打印word_Python使用扩展库pywin32实现批量文档打印实例

本文代码需要正确安装Python扩展库pywin32&#xff0c;建议下载whl文件进行离线安装。然后调用win32api的ShellExecute()函数来实现文档打印&#xff0c;系统会根据文档类型自动选择不同的软件进行打开并自动打印&#xff0c;如果要打印的是图片的话&#xff0c;需要手工确认一…

【牛客 - 283E】贪心只能过样例(模拟)

题干&#xff1a; 小西是单身狗&#xff0c;所以她不喜欢看到有CP在秀恩爱&#xff01; 有一天&#xff0c;小西出来闲逛&#xff0c;发现街上的行人都排成了一排&#xff0c;并且可以用这种形式表示&#xff1a; MMFMMFFFMMM 其中M表示男孩子&#xff0c;F表示女装的男孩…

apmserver导入MySQL_mysql数据库导入导出

window下1.导出整个数据库mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldump -u dbuser -p dbname > dbname.sql2.导出一个表mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名mysqldump -u dbuser -p dbname users> dbname_users.sql3.导出一个数据库结…

【牛客 - 283C】模拟只会猜题意(简单模拟)

题干&#xff1a; 小西突然觉得回文串是一种非常优雅的东西&#xff0c;她突然想要把身边所有的字符串都变成回文&#xff01; 所谓回文串就是一个倒置后仍与自身相等的字符串&#xff0c;如“gxuacmmcauxg”和“gxuacmcauxg”。 小西不喜欢单身狗&#xff0c;所以小西只会…

sql开启mysql远程连接_SQLServer2008设置开启远程连接

SQLServer2008设置开启INTERNET远程连接 SQL Server 2008默认是不允许远程连接的&#xff0c;sa帐户默认禁用的&#xff0c;如果想要在本地用SSMS连接远程服务器上的SQL Server 2008&#xff0c;需要做两个部分的配置&#xff1a; 使用sa账户登录SQL Server Management Studio(…

【牛客 - 283H】图论一顿套模板(思维转化,Dijkstra)

题干&#xff1a; 由于临近广西大学建校90周年校庆&#xff0c;西大开始了喜闻乐见的校园修缮工程&#xff01; 然后问题出现了&#xff0c;西大内部有许许多多的道路&#xff0c;据统计有N栋楼和M条道路&#xff08;单向&#xff09;&#xff0c;每条路都有“不整洁度”W&…

spss相关性分析看结果_spss相关性分析

当我们想要了解变量的相关程度时,就需要用到相关分析,而相关分析也是回归之前很重要的一步,通常用到的方法是pearson方法。 首先解释一下相关系数,相关系数反应的是两个变量之间变化趋势的方向以及程度,其值范围为-1到+1,正值表示正相关,负值表示负相关,绝对值越大表示…

【牛客 - 283F】出装方案(最小费用最大流)

题干&#xff1a; 众所周知&#xff0c;在各种对抗类游戏里装备都是很重要的一环&#xff0c;不同的出装方案会给玩家带来不同的强度。 dalao手里有N件装备&#xff0c;现在dalao要把装备分给N个队友&#xff0c;每个队友只能分一件装备&#xff0c;而每个队友穿上不同的装…