日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

【CodeForces - 278C 】Learning Languages(并查集,思维)

發(fā)布時(shí)間:2023/12/10 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【CodeForces - 278C 】Learning Languages(并查集,思维) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題干:

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

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個(gè)人,一共官方有M種語言,其中已知每個(gè)人會(huì)的語言,如果兩個(gè)人會(huì)同一種語言,那么兩個(gè)人就可以相互交流,如果A會(huì)語言1,2,B會(huì)語言2,3 C會(huì)語言3 4,那么B可以給C翻譯A說的話,說以這時(shí)候根據(jù)這個(gè)特性,那么ABC三個(gè)人就可以相互交流了。

公司為了讓這N個(gè)人都能夠相互交流,可以使得一些人學(xué)會(huì)一門語言,對應(yīng)花費(fèi)為1,問最少花費(fèi)多少能夠達(dá)到目的。

解題報(bào)告:

? ?剛開始想著枚舉每一個(gè)人,,然后看其他人有沒有一種語言可以與他配對,,,如果都不能配對就ans+1。。。。其實(shí)是不對的,,因?yàn)槟氵@樣跑出來還是有可能不會(huì)使圖是連通的。。。題意抽象出來就是求聯(lián)通塊的個(gè)數(shù)吧,然后添加最少邊使圖聯(lián)通啊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++) {//遍歷每一個(gè)人 int up = ss[i].size(); // int flag=0; // for(set<int>::iterator it = ss[k].begin(); it != ss[k].end(); ++it) {//遍歷這個(gè)人會(huì)的每一種語言 // 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 ;}

總結(jié):

? ?遇到問題還是要想著先建模,,不要著急敲代碼,,預(yù)處理的模型建好了思路才清晰、、

總結(jié)

以上是生活随笔為你收集整理的【CodeForces - 278C 】Learning Languages(并查集,思维)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。