[Codeforces Round #170 Div. 1] 277A Learning Languages
standard input
standard output
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).
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.
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).
5 5
1 2
2 2 3
2 3 4
2 4 5
1 5
0
8 7
0
3 1 2 3
1 1
2 5 4
2 6 7
1 3
2 7 4
1 1
2
2 2
1 2
0
1
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种语言,每个员工会某几种语言,问所有人至少再学总共多少种语言,可以让所有人之间可以直接或者间接地互相交流
注意:可能存在某个ZZ公司,里面的ZZ员工全部都一种语言也不会说。
并查集解决:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int mxn=;
int n,m;
int fa[mxn];
vector<int>e[mxn];//记录会每种语言的人
bool v[mxn];
int find(int x){
if(fa[x]==x)return x;
return fa[x]=find(fa[x]);
}
int main(){
int i,j;
scanf("%d%d",&n,&m);
int c,d;
bool flag=;
for(i=;i<=n;i++)fa[i]=i;
for(i=;i<=n;i++){
scanf("%d",&c);
if(c)flag=;
for(j=;j<=c;j++){
scanf("%d",&d);
e[d].push_back(i);
}
}
if(!flag){//如果所有人都一门语言也不会说,输出n
cout<<n<<endl;
return ;//忘了加return,多WA了3遍,233
}
for(i=;i<=m;i++)
for(j=;j<e[i].size();j++){
int x=find(e[i][j]);
int y=find(e[i][j-]);
//会说同一种语言的合并起来
if(x!=y)fa[x]=y;
}
for(i=;i<=n;i++)v[find(i)]=;
int ans=;
for(i=;i<=n;i++)if(v[i])ans++;
printf("%d\n",ans-);
return ;
}
[Codeforces Round #170 Div. 1] 277A Learning Languages的更多相关文章
- Codeforces Round #170 (Div. 2)
A. Circle Line 考虑环上的最短距离. B. New Problem \(n\) 个串建后缀自动机. 找的时候bfs一下即可. C. Learning Languages 并查集维护可以沟 ...
- Codeforces Round #170 (Div. 1 + Div. 2)
A. Circle Line 考虑环上的最短距离. B. New Problem \(n\) 个串建后缀自动机. 找的时候bfs一下即可. C. Learning Languages 并查集维护可以沟 ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
- Codeforces Round #262 (Div. 2) 1004
Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...
随机推荐
- Python SciPy Sparse模块学习笔记
1. sparse模块的官方document地址:http://docs.scipy.org/doc/scipy/reference/sparse.html 2. sparse matrix的存储 ...
- Js笔记-第17课
课 // 作业 //深度拷贝 var obj = { name:'rong', age:'25', card:['visa','alipay'], nam :['1','2','3','4','4'] ...
- iOS 骰子战争 Dice Wars
占坑 这个游戏之前在网页端玩过,App Store 上没有搜到特别好的,想自己做个类似的iOS APP 游戏 目测实现难度适中,可玩性较高
- 解决Windows 与Mac 双系统下的蓝牙设备共用的问题
不知道有多少人和我一样用的蓝牙鼠标或者键盘,有的话应该都会遇到同一个问题:即在一个系统下配好对后在另一个系统必须重新配对才能使用,很是麻烦.还要将蓝牙设备进入发现模式,OS下搜索,连接....终于昨天 ...
- 【数学 BSGS】bzoj2242: [SDOI2011]计算器
数论的板子集合…… Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最 ...
- (71)Received empty response from Zabbix Agent问题解决
刚接触zabbix新手少部分会出现如下错误: Received empty response from Zabbix Agent at [192.168.1.2]. Assuming that age ...
- python爬虫用到的一些东西
原装requests >>> import requests >>> response = requests.get('http://www.baidu.com') ...
- Python9-事件及队列-day37
信号量 from multiprocessing import Process from multiprocessing import Semaphore import time import ran ...
- HUD:2853-Assignment(KM算法+hash)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2853 Assignment Time Limit: 2000/1000 MS (Java/Others) ...
- Vmware安装与使用
1)双击击VMware Workstation启动安装 2)接下来Ctrl+N快捷键(或者点选菜单Flie->New virtual machine)来创建一个新的虚拟机. 3)Custom(a ...