CodeForces 278C Learning Languages

題目:

The "BerCorp" company has got n employees. These employees can use mapproved 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).
Example
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種語(yǔ)言,輸入每名員工會(huì)多少種語(yǔ)言以及會(huì)的語(yǔ)言的編號(hào)(有可能有員工一門(mén)都不會(huì))。現(xiàn)在公司可以給員工培訓(xùn)語(yǔ)言,但培訓(xùn)一個(gè)就要花1個(gè)幣。問(wèn)公司最少花多少錢(qián)可以保證這n名員工可以直接或間接(可以通過(guò)別人翻譯,但翻譯的人需懂那兩個(gè)人的語(yǔ)言)進(jìn)行交流。

思路:并查集。

參考代碼:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
using namespace std;
const int N = 100+5;

map<int, int> v[N];//對(duì)應(yīng)語(yǔ)言, 該員工是否學(xué)習(xí)了這門(mén)語(yǔ)言;
int par[N];
bool flag = true;//檢查是否所有人都不會(huì)任何一種語(yǔ)言, 如果是那么就給所有人教同一門(mén)語(yǔ)言;//true為是;

void init() {
    for (int i = 0;i < N;++i) par[i] = i;
}

void input(const int n) {
    int num;//每個(gè)人會(huì)的語(yǔ)言數(shù);
    int lan;//每個(gè)人會(huì)的語(yǔ)言的編號(hào);
    for (int i = 1;i <= n;++i) {
        cin >> num;
        if (num > 0) flag = false;
        for (int j = 1;j <= num;++j) {
            cin >> lan; 
            v[i][lan] = 1;//表明第i名員工會(huì)lan這門(mén)語(yǔ)言;
        }
    }
}
//并查集;
int finds(int x) {
    if (x == par[x]) return x;
    else return par[x] = finds(par[x]);
}

void unite(int x, int y) {
    int x1 = finds(x);
    int y1 = finds(y);
    if (x1 != y1) {
        par[y1] = x1;
    }
}

int judge(const int n, const int m) {
    if (flag == true) return n;
    for (int k = 1;k <= m;++k) {
        for (int i = 1;i <= n;++i) {
            for (int j = 1;j <= n;++j) {
                if (v[i][k] == 1 && v[j][k] == 1) {//代表他們兩個(gè)都會(huì)k這門(mén)語(yǔ)言;
                    unite(i, j);
                }
            }
        }
    }
    //for (int i = 1;i <= n;++i) {
    //  cout << par[i] << " ";//可以直接或間接交流的人用并查集并為一個(gè)集合;
    //}
    //cout << endl;
    int ans = 0;
    for (int i = 1;i <= n;++i) {
        if (par[i] == i) {
            ++ans;
        }
    }
    //cout << ans << endl;
    //如果有合并情況, 兩個(gè)集合中任意一個(gè)集合中的任意一個(gè)人學(xué)會(huì)另外一個(gè)集合中任意一個(gè)人會(huì)的語(yǔ)言即可;
    return ans - 1;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n, m;
    cin >> n >> m;
    init();
    input(n);
    int ans = judge(n, m);
    cout << ans << endl;
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,170評(píng)論 0 23
  • 瓜田李下 我兀自拾履 兀自正冠 向你靠近的一瞬 我就走完了一生一世 你像是一陣風(fēng) 吹拂在我的心頭上 無(wú)形化雨 我像...
    我在十一點(diǎn)半閱讀 340評(píng)論 9 3
  • 同班同學(xué)走向社會(huì),很快發(fā)現(xiàn),有些人立刻就找到了工作,而且都是好工作。有些人找死也沒(méi)有找到理想的。 你沒(méi)法知道一個(gè)人...
    花色春秋閱讀 268評(píng)論 0 0
  • 終于不再敢去對(duì)一個(gè)人掏心掏肺,因?yàn)椴灰?,朋友也有朋友。再也不敢去那么勇敢的去?ài),不是我不再相信愛(ài)情,沒(méi)有誰(shuí)離不...
    AShine_Gao閱讀 280評(píng)論 0 1
  • 文:王小雪 時(shí)間疾走 我們走在路上 步履蹣跚 有時(shí)我們會(huì)走到自己前面去 偶爾會(huì)回頭望一望 等我們 慢慢把速度調(diào)整回...
    王小雪的秘密花園閱讀 523評(píng)論 0 2

友情鏈接更多精彩內(nèi)容