A. Learning Languages
time limit per test:2 seconds
memory limit per test:256 megabytes


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

Sample test(s)
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.

题目链接:http://codeforces.com/problemset/problem/277/A

题目大意:n个员工。m种语言。每一个员工会k种语言,问总共还要学习多少语言,能够让每两个员工能够直接或者间接交流

题目分析:非常明显的并查集问题,注意当k等于0时,该员工必学一门语言。具体见程序凝视

#include <cstdio>
#include <cstring>
int a[105], fa[105]; void UF_set()
{
for(int i = 0; i < 105; i++)
fa[i] = i;
} int Find(int x)
{
return x == fa[x] ? x : fa[x] = Find(fa[x]);
} void Union(int a, int b)
{
int r1 = Find(a);
int r2 = Find(b);
if(r1 != r2)
fa[r2] = r1;
} int main()
{
int n, m, cnt = 0, ans = 0; //ans表示理论上须要学习的人数,cnt表示没人学的
scanf("%d %d", &n, &m);
memset(a, 0, sizeof(a));
UF_set();
for(int i = 0; i < n; i++)
{
int k, fir, next;
scanf("%d", &k);
if(k == 0) //若一门语言都不会。则必要学一门
{
ans++;
continue;
}
scanf("%d", &fir);
a[fir] ++;
for(int i = 1; i < k; i++)
{
scanf("%d", &next);
a[next]++;
Union(fir, next);
}
}
for(int i = 1; i <= m; i++)
{
if(a[i] == 0) //记录没人学的
cnt++;
if(fa[i] == i) //记录集合个数
ans++;
}
//没人学的必定自成一个集合,我们要把它减去,由于既然没一个人学
//它就不须要再被学,若ans等于m说明每两个人之间不能交流。则每人都要学习
//一门语言,否则拿集合个数减去没人学的语言个数再减1,这里减1的含义是
//随意n个集合,n-1条线就能将其连通
printf("%d\n", (cnt == m) ? n : ans - cnt - 1);
}

版权声明:本文博主原创文章。博客,未经同意不得转载。

CodeForces 277A Learning Languages (并检查集合)的更多相关文章

  1. [Codeforces Round #170 Div. 1] 277A Learning Languages

    A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes input standa ...

  2. Codeforces 278C Learning Languages(并查集)

    题意抽象出来就是求联通块的个数吧,然后添加最少边使图联通. 注意所有人都不会任何语言的时候,答案是n而不是n-1. #include<algorithm> #include<iost ...

  3. BZOJ3296: [USACO2011 Open] Learning Languages

    3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 81  Solved: ...

  4. HDU 1272 小希迷宫(并检查集合)

    意甲冠军:被判处无向图无环和连接无处不在 思考:并检查集合,trap 您可能有一个直接输入0 0 并且....合并的时候按某一个方向会爆栈,爆了好几次...下次考虑一下直接递归找祖先吧 #includ ...

  5. BZOJ3296:Learning Languages(简单并查集)

    3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 436  Solved ...

  6. C. Learning Languages 求联通块的个数

    C. Learning Languages 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring&g ...

  7. codeforces 277 A Learning Languages 【DFS 】

    n个人,每个人会一些语言,两个人只要有会一门相同的语言就可以交流,问为了让这n个人都交流,至少还得学多少门语言 先根据n个人之间他们会的语言,建边 再dfs找出有多少个联通块ans,再加ans-1条边 ...

  8. hdu1325 Is It A Tree?并检查集合

    pid=1325">职务地址 试想一下,在词和话题hdu1272是一样的. 可是hdu1272的博文中我也说了.数据比較水,所以我用非并查集的方法就AC了. 可是这题的数据没那么水,要 ...

  9. URAL - 1966 - Cycling Roads(并检查集合 + 判刑线相交)

    意甲冠军:n 积分,m 边缘(1 ≤ m < n ≤ 200),问:是否所有的点连接(两个边相交.该 4 点连接). 主题链接:http://acm.timus.ru/problem.aspx? ...

随机推荐

  1. Hdu 5256 系列转换

    主题链接: HDU5236 代码: #include<iostream> #include<cstdio> #include<cstring> #include&l ...

  2. ROW_NUMBER() OVER 排序函数的基本用法

    ROW_NUMBER() OVER 排序函数 select ROW_NUMBER() OVER (PARTITION BY guide_id ORDER BY pic_sort) as rowid,p ...

  3. poj 1959 Darts 同意反复组合

    水题.直接贴代码. //poj 1959 //sep9 #include <iostream> using namespace std; int n; int f[128]; int so ...

  4. jdk和jre是什么?都有什么用?

    大家肯定在安装JDK的时候会有选择是否安装单独的jre,一般都会一起安装,我也建议大家这样做.由于这样更能帮助大家弄清楚它们的差别: Jre   是java   runtime   environme ...

  5. ProgressBar样式总结与自己主动填充方法(代码)

    有时候开发的时候须要用一个进度条告知用户眼下正在执行一个耗时操作,可是并不须要明白知道某个value来setProgress,所以就能够自己定义一个时间和进度让进度条自己主动执行了. 以下是代码: H ...

  6. ASIHTTPRequest 对GET POST 请求简包

    1.ASIHTTPRequest一个简短的引论 github下载链接https://github.com/pokeb/asi-http-request 2.ASIHTTPRequest 对GET和PO ...

  7. [Unity3D] 有关公告板实现的误区

    最直接实现一个公告板,我认为我们应该这样做: usingUnityEngine; publicclassBillboard :MonoBehaviour { voidUpdate() { transf ...

  8. android关键知识

    1.handler 与Looper 与MessageQueue .Message关系 handler:是处理主线程(ui线程)处理耗时操作的线程,通过post message到MessageQueue ...

  9. C# 经典排序算法大全

    C# 经典排序算法大全 选择排序 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  10. Java 审查基调

    1.& 与 &&的差别 两个都有逻辑与的功能. 可是所不同的是.当&两边的表达式不是boolean类型的时候,&具有位与的功能:&&是短路与,当 ...