[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 ...
随机推荐
- 有关SQL的一道面试题
这是一个学生分数表 StudentName StudySubject SubjectScore Peter ...
- Mysql常用运算符与函数汇总
Mysql常用运算符与函数汇总 本文给大家汇总介绍了mysql中的常用的运算符以及常用函数的用法及示例,非常的全面,有需要的小伙伴可以参考下 我们先把数据表建好 use test;create tab ...
- matplotlib绘图(三)
matplotlib中2D图形的绘制 直方图 直方图的参数只有一个x,不像条形图需要传入x,y 直方图作用:是统计x在某个区间上出现的次数 直方图是条形图的一种形式 hist()的参数: #bins ...
- Linux菜鸟起飞之路【十】进程管理
一.基本概念 进程:程序的一次动态执行 守护进程:在后台运行并提供系统服务的进程 僵尸进程:完成了生命周期但却依然留在进程表中的进程,我们称之为 “僵尸进程” 父进程:指已创建一个或多个子进程的进程. ...
- 基于Centos7.2使用Cobbler工具定制化批量安装Centos7.2系统
1.1 定制Centos_7_x86_64.ks文件内容 # Cobbler for Kickstart Configurator for CentOS 7.2.1511 by Wolf_Dre ...
- 【netbeans】netbeans utf-8编码
首先,在你的netbeans的安装文件夹里面找到etc这个文件夹,打开,在里面找到netbeans.conf这个文件,打开,找到这一句netbeans_default_options="-J ...
- python基本操作(五)
if 判断 if 条件: 代码1 代码2 代码3 代码块(同一缩进级别的代码,例如代码1.代码2和代码3是相同缩进的代码,这三个代码组合在一起就是一个代码块,相同缩进的代码会自上而下的运行) cls ...
- Python json和simplejson的使用
在Python中,json数据和字符串的转换可以使用json模块或simplejson模块. json从Python2.6开始内置到了Python标准库中,我们不需要安装即可直接使用. simplej ...
- 使用TensorFlow的卷积神经网络识别手写数字(2)-训练篇
import numpy as np import tensorflow as tf import matplotlib import matplotlib.pyplot as plt import ...
- LeetCode(117) Populating Next Right Pointers in Each Node II
题目 Follow up for problem "Populating Next Right Pointers in Each Node". What if the given ...