ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

NC24608 [USACO 2011 Ope S]Learning Languages

2022-07-10 19:05:41  阅读:214  来源: 互联网

标签:language int Contessa USACO Languages 集合 cows NC24608 speak


题目链接

题目

题目描述

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+k))\)

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

代码

#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;
}

标签:language,int,Contessa,USACO,Languages,集合,cows,NC24608,speak
来源: https://www.cnblogs.com/BlankYang/p/16463712.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有