题目链接

题目

题目描述

Farmer John's N (2 <= N <= 10,000) cows, conveniently numbered 1..N, are fluent in some M (1 <= M <= 30,000) languages, also conveniently numbered from 1..M. Cow i can speak in KiK_iKi (1 <= \(K_i\) <= M) languages, namely \(L_{i_1}, L_{i_2},..., L_{i_{K_i}}\) (1 <= \(L_{i_j}\) <= M). FJ's cows aren't THAT smart, so the sum of \(K_i\) over all cows i is at most 100,000.

Two cows can't directly talk to each other unless both speak a common language. However, cows can pass messages along, translating if necessary. In other words, cows A and B can have a conversation if and only if there exists a sequence of cows \(T_1, T_2, ..., T_k\)​ such that A and \(T_1\)​ share a language, \(T_1\)​ and \(T_2\)​ share a language, etc., and \(T_k\)​ and B share a language.

Farmer John wishes that his cows could be even more social, so he wants all his cows to be able to socialize with any other cow. He can buy books to teach any one of his cows any language he pleases. Being a fairly frugal farmer, FJ wants to purchase the minimum number of books necessary to enable all of his cows to speak to each other. Help him determine:

* The minimum number of books he must purchase

* Any set of books assigned to cows in any order which will help him meet this goal; a program will grade your output.

By way of example, suppose there are three cows named Alberta, Bessie, and Contessa along with three languages denoted as #1, #2, and #3. Alberta can speak languages #2 and #3, Bessie can speak language #2, and Contessa can speak language #1. Currently, Alberta and Bessie can talk to each other, but Contessa is left alone.

#1 #2 #3

Alberta x x

Bessie x

Contessa x

FJ wants to fix this situation, so he can buy Contessa a book to teach her language #2. This will ensure all cows speak the same language, so they can all communicate with one another.

Note that an alternate solution exists: instead, FJ could buy

Contessa a book to teach her language #3. Not all cows would speak the same language, but this would still be a valid solution because Contessa could communicate through Alberta (who also speaks language #3) if she wants to talk to Bessie. Other alternatives exist, and any valid alternate solution will also be accepted.

输入描述

  • Line 1: Two space-separated integers: N and M
  • Lines 2..N+1: Line i+1 describes the languages that cow i can speak with Ki+1K_i+1Ki+1 space-separated integers: \(K_i\), \(L_{i_1}, L_{i_2},..., L_{i_{K_i}}\).

输出描述

  • Line 1: A single integer that is the minimum number of books that FJ must purchase.
  • Lines 2..B+1: Line i+1 contains two space-separated integers: the language id # and the id # of the cow to receive book i. If multiple solutions exist, print any one.

示例1

输入

3 3
2 3 2
1 2
1 1

输出

1

题解

知识点:并查集。

本题显然用并查集,但需要做扩展域。

牛与牛之间关系不是简单联系的,而是通过语言种类作为桥梁。因此将语言种类集合并入牛集合作为合并的桥梁集合,只要牛通过语言桥梁集合与另一只牛连通,即能够交流。

具体上,在牛的集合 \([1,n]\) 后加入语言集合 \([n+1,m]\) 即可,每次合并务必用牛作为根节点,是为了防止有些语言所有牛不会单独成为集合,干扰有效集合计数。只要把牛作为根节点,就只需要在 \([1,n]\) 计数,不会产生桥梁集合单独存在的问题。

最后牛区间中不同集合的数量减一,就是要连接的(牛->语言)边的数量。

时间复杂度 \(O(nk\log (n+m)+m)\)

空间复杂度 \(P(n+m)\)

代码

#include <bits/stdc++.h>

using namespace std;

int fa[40007];///牛与书的扩展集合

int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
} void merge(int x, int y) {
fa[find(y)] = find(x);
} int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1;i <= n + m;i++) fa[i] = i;
for (int i = 1;i <= n;i++) {
int k;
cin >> k;
while (k--) {
int l;
cin >> l;
merge(i, l + n);
}
}
int ans = 0;
for (int i = 1;i <= n;i++) {///语言可能多出来
if (fa[i] == i) ans++;
}
cout << ans - 1 << '\n';
return 0;
}

NC24608 [USACO 2011 Ope S]Learning Languages的更多相关文章

  1. BZOJ3296: [USACO2011 Open] Learning Languages

    3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 81  Solved: ...

  2. CodeForces 277A Learning Languages (并检查集合)

    A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes The "Be ...

  3. BZOJ3296:Learning Languages(简单并查集)

    3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 436  Solved ...

  4. [Codeforces Round #170 Div. 1] 277A Learning Languages

    A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes input standa ...

  5. C. Learning Languages 求联通块的个数

    C. Learning Languages 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring&g ...

  6. NC25136 [USACO 2006 Ope B]Cows on a Leash

    NC25136 [USACO 2006 Ope B]Cows on a Leash 题目 题目描述 给定如图所示的若干个长条.你可以在某一行的任意两个数之间作一条竖线,从而把这个长条切开,并可能切开其 ...

  7. [USACO 2011 Nov Gold] Cow Steeplechase【二分图】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=93 很容易发现,这是一个二分图的模型.竖直线是X集,水平线是Y集,若某条竖 ...

  8. [USACO 2011 Nov Gold] Above the Median【逆序对】

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=91 这一题我很快的想出了,把>= x的值改为1,< x的改为- ...

  9. USACO 2011 February Silver Cow Line /// 康拓展开模板题 oj22713

    题目大意: 输入n k,1-n的排列,k次操作 操作P:输入一个m 输出第m个排列 操作Q:输入一个排列 输出它是第几个排列 Sample Input 5 2P3Q1 2 5 3 4 Sample O ...

  10. Codeforces 278C Learning Languages(并查集)

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

随机推荐

  1. 打 multi-fidelity RL 旗号,但是幼稚监督学习 + 迁移学习

    文章名称:Multi-fidelity reinforcement learning framework for shape optimization 链接:https://www.sciencedi ...

  2. xshell配置隧道转移规则

    钢铁知识库,一个学习python爬虫.数据分析的知识库.人生苦短,快用python. xshell是什么 通俗点说就是一款强大ssh远程软件,可以方便运维人员对服务器进行管理操作,功能很多朋友们自行探 ...

  3. React报错之Property does not exist on type 'JSX.IntrinsicElements'

    正文从这开始~ 总览 当组件名称以小写字母开头时,会导致"Property does not exist on type 'JSX.IntrinsicElements'"错误.为了 ...

  4. Java项目配置Maven依赖时不知需要的最低jdk版本?(报错java: 错误: 无效的目标发行版:17)

    1.问题 在配置SpringBoot项目依赖时,使用了最新的spring-boot-starter-parent 3.1.5,但是出现了java: 错误: 无效的目标发行版:17的报错 2.解决 经过 ...

  5. 使用VS开发人员工具观察类在内存中的布局

    1.先要生成相应文件 2.打开VS2019开发人员工具 3.cd至文件目录 4.输入cl /d1 reportSingleClassLayoutanimal demo.cpp 其中reportSing ...

  6. [转帖]shell编程:变量知识进阶(三)

    https://www.cnblogs.com/luoahong/articles/9154309.html 1 Shell特殊位置变量 范例1:$n的实践例子 1 2 3 4 5 6 7 8 9 1 ...

  7. Oceanbase开源版 数据库恢复MySQL数据库的过程

    # Oceanbase开源版 数据库恢复MySQL数据库的过程 背景 想进行一下Oceanbase数据库的兼容性验证. 想着用app create 数据库的方式周期比较长. 所以我想着换一套 备份恢复 ...

  8. [转帖]谁动了我的 CPU 频率 —— CPU 性能之迷 Part 2

    https://blog.mygraphql.com/zh/notes/low-tec/kernel/cpu-frequency/ 目录: 为何有本文 什么是动态 CPU 频率 什么是 p-state ...

  9. [转帖]Linux——Shell脚本参数传递的2种方法

    https://www.cnblogs.com/caoweixiong/p/12334418.html 前言 平时会遇到很多脚本都有参数选项,类似: ./test.sh -f config.conf ...

  10. 【转帖】《MySQL高级篇》四、索引的存储结构

    1. 为什么使用索引 假如给数据使用 二叉树 这样的数据结构进行存储,如下图所示 2.索引及其优缺点 2.1 索引概述 2.2 优点 类似大学图书馆建书目索引,提高数据检索的效率,降低 数据库的 IO ...