【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 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
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).
Examples
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
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.
題目大意:
公司一共有N個人,一共官方有M種語言,其中已知每個人會的語言,如果兩個人會同一種語言,那麼兩個人就可以相互交流,如果A會語言1,2,B會語言2,3 C會語言3 4,那麼B可以給C翻譯A說的話,說以這時候根據這個特性,那麼ABC三個人就可以相互交流了。
公司為了讓這N個人都能夠相互交流,可以使得一些人學會一門語言,對應花費為1,問最少花費多少能夠達到目的。
解題報告:
剛開始想著列舉每一個人,,然後看其他人有沒有一種語言可以與他配對,,,如果都不能配對就ans+1。。。。其實是不對的,,因為你這樣跑出來還是有可能不會使圖是連通的。。。題意抽象出來就是求聯通塊的個數吧,然後新增最少邊使圖聯通啊mmp。
AC程式碼:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
int a[102][102][102];
int f[102];
set<int> ss[102];
int getf(int v) {
return v == f[v] ? v : f[v] = getf(f[v]);
}
void merge(int u,int v) {
int t1 = getf(u);
int t2 = getf(v);
if(t1 != t2) f[t2]=t1;
}
int main()
{
int n,m;
int ans=0,fl=0;
cin>>n>>m;
for(int i = 1; i<=n; i++) f[i]=i;
for(int i = 1,num,tmp; i<=n; i++) {
scanf("%d",&num);
if(num>0) fl=1;
while(num--) {
scanf("%d",&tmp);
ss[i].insert(tmp);
}
}
if(fl==0) {
printf("%d\n",n);return 0 ;
}
for(int k = 1; k<=m; k++) {
for(int i = 1; i<=n; i++) {
for(int j = 1; j<=n; j++) {
if(ss[i].find(k) != ss[i].end() && ss[j].find(k) != ss[j].end()) merge(i,j);//a[i][j][k]=a[j][i][k]=1;
}
}
}
for(int i = 1; i<=n; i++)
if(f[i] == i) ans++;
// for(int i = 1; i<=n; i++) {
// int flag = 0;
// for(int j = 1; j<=n; j++) {
// if(i==j) continue;
// for(int k = 1; k<=m; k++) {
// if(a[i][j][k] == 1) {
// flag=1;break;
// }
// }
// if(flag==1) break;
// }
// if(flag==0) ans++;
// }
printf("%d\n",ans-1);
// for(int k = 1; k<=n; k++) {//遍歷每一個人
//// int up = ss[i].size();
// int flag=0;
// for(set<int>::iterator it = ss[k].begin(); it != ss[k].end(); ++it) {//遍歷這個人會的每一種語言
// for(int i = 1; i<=n; i++) {
// if(i==k) continue;
// if(ss[i].find(*it) != ss[i].end()) {
// flag=1;break;
// }
// }
// }
// if(flag == 0) ans++;
// }
// printf("%d\n",ans/2);
return 0 ;
}
總結:
遇到問題還是要想著先建模,,不要著急敲程式碼,,預處理的模型建好了思路才清晰、、