CodeForces - 278C Learning Languages

文章描述了一个公司中员工使用不同语言进行正式沟通的问题,需要找出最小的额外学习语言花费使得所有员工能相互通信。解决方案是利用并查集算法找到员工语言的连通块,计算出连通块的数量减一即为所需学习的新语言数量。给出的C++代码实现了该算法来求解最小花费。
摘要由CSDN通过智能技术生成

题干:

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

Note:

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.

题意:

给出总共多少个员工和多少种语言,然后每个员工给出对应数量的语言的种类。询问需要再学习多少种语言才能使员工们能够沟通。

思路:

使员工们能互相沟通,换言之就是最少有一条通路,只需要找出员工连通块的个数-1即可。俩个员工彼此掌握的语言互相匹配,如果中间有相同的那么就可以连通。

代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=105;
const int mod=998244353;
set<int> s[N];
ll ans;
int f[N];
//并查集模板
int getf(int x){
    if(f[x]==x) return x;
    else return f[x]=getf(f[x]);
}
void merge(int x,int y){
    f[getf(x)]=getf(y);
}
inline int iread(){
    int ans=0,sign=1;
    char ch = getchar();
    while(ch<'0'||ch>'9'){
        if(ch==' '){
            sign=-1;
        }
        ch = getchar();
    }
    while(ch>='0'&&ch<='9'){
        ans = ans*10 + ch-'0';
        ch = getchar();
    }
    return ans*sign;
}
int n,m;
int main(){
    n=iread(),m=iread();
    for(int i=1;i<=n;i++){
        f[i]=i;
    }
    int flag=0;
    //存储每个员工掌握的语言
    for(int i=1;i<=n;i++){
        int num;
        num=iread();
        if(num>0) flag=1;
        for(int j=1;j<=num;j++){
            int x;
            x=iread();
            s[i].insert(x);
        }
    }//判断是否都不连通
    if(flag==0){
        cout<<n;
        return 0;
    }
//    for(int i=1;i<=n;i++){
//        cout<<i<<":";
//        for(set<int>::iterator item=s[i].begin();item!=s[i].end();item++){
//            cout<<"item:"<<*item<<endl;
//        }
//    }
    //2个循环将员工每队都进行比较
    for(int i=1;i<n;i++){
        for(int j=i+1;j<=n;j++){
            //遍历俩个员工掌握的语言
            for(set<int>::iterator item=s[i].begin();item!=s[i].end();item++){
                for(set<int>::iterator item2=s[j].begin();item2!=s[j].end();item2++){
                    if(*item==*item2){
                        merge(i,j);
                        break;
                    }
                }
            }
        }
    }
    //判断连通块数量
    for(int i=1;i<=n;i++){
        if(f[i]==i){
            ans++;
        }
    }
    cout<<ans-1;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱吃芒果的蘑菇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值