Codeforces 278C. Learning Languages

避坑指南:雅特力AT32F403A在Keil5中的V2库配置常见问题解析 本文详细解析了国产MCU雅特力AT32F403A在Keil5中配置V2库的常见问题,包括工程创建、库文件配置、编译错误和外设设置等关键环节。针对项目文件夹结构混乱、芯片型号选择错误、头文件路径设置不当等典型问题提供实用解决方案,帮助开发者高效避坑,提升开发效率。 阅读详情
Learning Languages Time Limit:2000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

Description

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 Input

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

Hint

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种语言,要想让所有人能直接或间接沟通,至少需要学多少人次。


解法:只需要用并差集找出已经形成集合的个数n,那么还需要n-1就能把整个合并成一个集合,然后加上一门

语言都不会的人数即可,需要注意磁体有特数据,所有人会的语言数都为0的情况。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
const int maxn=101;
int n,m,fa[maxn];
set<int> s[maxn];

int find(int x){
    return x==fa[x] ? x : fa[x]=find(fa[x]);
}

bool check(int i,int j){
    set<int>::iterator x=s[i].begin();
    while(x!=s[i].end()){
        int y=*x;
        if(s[j].find(y)!=s[j].end()) return true;
        x++;
        //cout << 1 << endl;
    }
    return false;
}

int main()
{
    scanf("%d %d",&n,&m);
    for(int i=0;i<=n;i++) fa[i]=i;
    int sum,x,ans=0;
    for(int i=0;i<n;i++){
        scanf("%d",&sum);
        if(sum==0) ans++;
        while(sum--){
            scanf("%d",&x);
            s[i].insert(x);
        }
    }
    if(ans==n) printf("%d\n",n);
    else{
        for(int i=0;i<n;i++)
            for(int j=i+1;j<n;j++)
                if(check(i,j)){
                    int fi=find(i),fj=find(j);
                    if(fi!=fj) fa[fi]=fj;
                }
        int ant=0;
        for(int i=0;i<n;i++) if(fa[i]==i) ant++;
        printf("%d\n",ant-1);
    }
    return 0;
}



arcgis插件 批量出图 按地块批量出图工具(动态文本、动态节点坐标表) arcgis插件 批量出图 按地块批量出图工具。 阅读详情

相关推荐

STC8H8K64U ADC采样实战:从蜂鸣器控制到电压测量(附完整代码)

本文以STC8H8K64U单片机为核心,通过两个实战案例详细讲解ADC采样的应用。首先介绍如何利用ADC采样值动态控制蜂鸣器响度,实现模拟PWM效果;其次深入探讨高精度外部电压测量的方法,包括硬件连接、两点校准技巧以及软件滤波,旨在帮助开发者从理论到实践全面掌握ADC功能。

weixin_29159441的博客 558

Codeforces 278C Learning Languages(并查集)

题意抽象出来就是求联通块的个数吧,然后添加最少边使图联通。 注意所有人都不会任何语言的时候,答案是n而不是n-1。 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define FF(i

diary_yang的专栏 1613

uniapp串口通信实战:从零搭建物联网设备控制App(附完整代码)

本文详细介绍了如何使用Uniapp实现串口通信,从零搭建物联网设备控制App。内容涵盖环境配置、串口初始化、数据收发、错误处理等核心功能,并提供了完整的代码示例,帮助开发者快速掌握跨平台串口通信技术,适用于智能农业、工业自动化等物联网场景。

weixin_30369041的博客 597

Codeforces Round #170 (Div. 2)---C. 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 w

Alex 1238

TS808效果器制作教程

TS808效果器制作教程 可以在立创商城购买到大部分的电阻电容和IC,音频接口,踩钉,电位器和电源接口可以去某宝看看都买得到的。链接https//pan.baidu.com/s/1QId1eIE3fJ2u5NLBSISvGA。安装过程过于繁琐,把所有元器件按照图纸焊接完成就可以调试了,祝你们成功!这是TS808经典效果器制作过程的记录。IC是JRC4558。...

qq_18948149的博客 3423

3D Slicer:从入门到精通(二)——入门指南

本篇文章包含开始使用3D Slicer所需的信息,包括如何安装和快速开始使用基本功能,以及在哪里找到更多信息。

walt3433的博客 2万+

Codeforces 278C Learning Languages【并查集】水题

C. Learning Languages time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The "BerCorp" company has got n employees. Thes

mengxiang000000 1388

IKM 测试题 - Java SE 8 评估测试题目

IKM 测试题 - Java SE 8 评估测试题目

licheng11y的博客 1313

A - Learning Languages CodeForces - 277A(并查集 Java实现)

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 ...

Michael Hsu 589

STM32开发 -- 4G模块开发详解(1)

这部分一直再看,资料都已经整理好了,但是一直没有汇总。接下来就详细的看一下,GSM这部分是怎么实现的。

不积跬步,无以至千里 5万+

告别手动读摘要!爬虫+大模型做学术助手:爬知网论文,10秒生成摘要+关键词(附完整代码)

知网的官方摘要往往冗长(200-300字),关键词可能不够精准,用大模型提炼“100字精简摘要”和“3-5个核心关键词”,更适合快速筛选文献。这套学术助手的核心价值,是把“找论文、读摘要、提关键词”这些重复劳动,交给爬虫和大模型自动化完成,让科研人从繁琐的文献整理中解放出来,把时间花在实验设计、数据分析等核心工作上。需要注意的是:爬取知网论文仅用于个人科研和学习,不得用于商业用途,遵守知网的用户协议和版权规定;大模型生成的摘要和关键词需后续核对原文,确保准确性。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1942

codeforce C. 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

Vision 692

《大数据最全面试题-Offer直通车》目录

做好这些不用担心试用期不通过:程序员入职新公司如何快速上手项目本人遇到大数据面试题和参考答案(超过1万字精华版)大数据面试英文自我介绍参考(万字长文)大数据运维应用场景面试题汇总及参考答案(持续更新)万字数据仓库面试题及参考答案数据仓库数据质量监控和处理方法最佳实践数据仓库建模方法万字详解Doris的3种数据模型详解和数据仓库每一层的模型选用大数据面试临阵磨枪不知看什么?看这份心理就有底了-大数据常用技术栈常见面试100道题最全Hive面试题2024年(2万字详解)Elasticsearch 面试题及参考答

大模型大数据攻城狮的专栏 2641

Codeforces 278C Learning Languages(并查集) 求连通块

Codeforces 278C Learning Languages(并查集) 求连通块 为什么最后还要getfather 一遍 比如 x 是 y 的父亲 然后你 Union(x,z) 然后 z 变成了 x 父亲 然后 y 的祖先就是错的了 题解 求一个无向图中有几个连通块 sum 特判 一下 如果 每一个人的语言都为 0 则答案为 sum 其他 答案则为 su...

weixin_33860147的博客 177

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

CodeForces - 278C Learning Languages

CodeForces - 278C Learning Languages

madtry的博客 192

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Codeforces 278C. Learning Languages 图的遍历

C. Learning Languages time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The "BerCorp" company has got n emplo

Azarath Metrion Zinthos 1322

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

codeforces 278C. Learning Languages(并查集)

C. Learning Languages time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The "BerCorp" company has got n employees. Thes

hahahaha 1010

【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 empl...

xuanweiace的博客 974

Codeforces Round #170 (Div. 2) C. Learning Languages【并查集】

http://codeforces.com/problemset/problem/278/C 一开始一门语言都不会的肯定都需要辅导其一门语言,会语言的用并查集处理,一共有几个集合,就辅导几减一的人。 #include <cstdio> #include <cstring> #include <iostream> #include <algor...

weixin_30501857的博客 76

Codeforces Round #170 (Div. 2) C. Learning Languages 并查集

链接: http://codeforces.com/contest/278/problem/C 题意: 有n个员工,m种语言,每名员工可能会几种语言,也可能0门, 问最少需要在学多少门语言,使得任意两个人可以直接或者间接沟通 题解: 不区分员工和语言,直接用并查集就行了,最后就是集合数-1 但是要注意如果所有的人都会0门语言的话,结果就是n 代码: 31 int n...

花海 194

CF图论一--Learning Languages--并查集

C. Learning Languages time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The "BerCorp" company has gotnemployees. These employees ...

lanshan1111的博客 302

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 emp

SIOFive的专栏 1235

CodeForces-Learning Languages

#include <stdio.h> #include <string.h> #include <stdlib.h> /* 这道题目考查并查集,特别注意每个人都不会语言的情况,此时结果为n。 */ int father[105]; int lang[105][105]; int flag[105]; int set[105]; int find(int x){ int r = x;

li8630的专栏 750

Codeforces Round #170 (Div. 2) C. Learning Languages(并查集)

C. Learning Languages time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The "BerCorp" company has got n employees...

weixin_34404393的博客 240

leecode278. 第一个错误的版本

纯粹笔记整理 题干 你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的。 假设你有 n 个版本 [1, 2, …, n],你想找出导致之后所有版本出错的第一个错误的版本。 你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在...

枠成 572
上一篇: POJ 1328 Radar Installation(预处理贪心)
下一篇: CodeForces 387C George and Number
hellohelloC
博客等级 码龄12年 15粉丝 107原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值