题意抽象出来就是求联通块的个数吧,然后添加最少边使图联通。

注意所有人都不会任何语言的时候,答案是n而不是n-1。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<fstream>
#include<sstream>
#include<vector>
#include<string>
#include<cstdio>
#include<bitset>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define REP(i, n) for(int i=0; i<n; i++)
#define CLR(a, b) memset(a, b, sizeof(a))
#define debug puts("**debug**")
#define LL long long
#define PB push_back
#define MP make_pair
#define eps 1e-8
using namespace std; const int maxn = 111;
int n, m, k, x, fa[maxn];
set<int> lg[maxn];
set<int> :: iterator it; int findset(int x) { return x == fa[x] ? x : fa[x] = findset(fa[x]); } bool check(int i, int j)
{
for(it=lg[i].begin(); it!=lg[i].end(); it++)
if(lg[j].find(*it) != lg[j].end()) return true;
return false;
} int main()
{
scanf("%d%d", &n, &m);
int cnt = 0;
FF(i, 1, n+1)
{
fa[i] = i;
scanf("%d", &k);
if(k == 0) cnt++;
while(k--)
{
scanf("%d", &x);
lg[i].insert(x);
}
}
if(cnt == n)
{
printf("%d\n", n);
return 0;
}
FF(i, 1, n+1) FF(j, i+1, n+1) if(check(i, j))
{
int x = findset(i), y = findset(j);
if(x != y) fa[x] = y;
}
int ans = 0;
FF(i, 1, n+1) if(fa[i] == i) ans++;
printf("%d\n", ans-1);
return 0;
}

Codeforces 278C Learning Languages(并查集)的更多相关文章

  1. BZOJ3296: [USACO2011 Open] Learning Languages 并查集

    Description 农夫约翰的N(2 <= N<=10,000)头奶牛,编号为1.. N,一共会流利地使用M(1<= M <=30,000)种语言,编号从1  .. M., ...

  2. CodeForces 277A Learning Languages (并检查集合)

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

  3. Codeforces Gym 100463E Spies 并查集

    Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...

  4. Codeforces 859E Desk Disorder 并查集找环,乘法原理

    题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...

  5. Codeforces - 828C String Reconstruction —— 并查集find()函数

    题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...

  6. Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)

    Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...

  7. CodeForces 455C Civilization (并查集+树的直径)

    Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...

  8. Codeforces 650C Table Compression (并查集)

    题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! #include <iostream> #includ ...

  9. Codeforces 468B Two Sets 并查集

    题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...

随机推荐

  1. hdu 2845(dp基础题)

    题意:容易理解. 分析:以后碰到这种类型的题,就要考虑把矩阵先按行来处理,再按列处理.先算出每行能够能够得到的最大值,然后按列处理即可. 代码实现: #include<stdio.h> # ...

  2. jquery的each()函数用法

    each()方法能使DOM循环结构简洁,不容易出错.each()函数封装了十分强大的遍历功能,使用也很方便,它可以遍历一维数组.多维数组.DOM, JSON 等等 在javaScript开发过程中使用 ...

  3. Tableau学习笔记之五

    计算用户自定义字段,虽然在Tableau软件中已经加入了很多的数值操作运算,比如平均值,最大值等,但是可以自定义自己需要的数值操作运算. 数值操作可以有以下:预定义函数,百分比,总计,分级等等 1.直 ...

  4. 提取数字、英文、中文、过滤重复字符等SQL函数(含判断字段是否有中文)

    --SQL 判断字段值是否有中文 create  function  fun_getCN(@str  nvarchar(4000))    returns  nvarchar(4000)      a ...

  5. redo文件三

    switch logfile是一种昂贵的操作,在进行日志切换的时候,是不允许生成新的redo信息 在前台进程生成redo日志信息的时候,此时redo buffer已经分配了空间,并且在当前的redo日 ...

  6. HTML 5:你必须知道的data属性

    原文:All You Need to Know About the HTML5 Data Attribute 译文:你必须知道HTML 5 的Data属性 译者:dwqs HTML 5的Data属性可 ...

  7. android各种适配器的用法(转)

    ArrayAdapter_SimpleAdapter_CursorAdapter的区别 数据源不同而已1. String[]: ArrayAdapter2. List<Map<String ...

  8. Codevs No.1163 访问艺术馆

    2016-05-31 20:48:47 题目链接: 访问艺术馆 (Codevs No.1163) 题目大意: 一个贼要在一个二叉树结构的艺术馆中偷画,画都处于叶子节点处,偷画和经过走廊都需要时间,求在 ...

  9. Visual Studio 2013智能提示失效解决办法

    各种解决VS2013智能提示失效办法: 1.重置所有设置    工具->导入导出设置->重置所有设置 2.智能提示开关: 工具->选项->文本编辑器->C#->常规 ...

  10. 基于 Red5 的流媒体服务器的搭建和应用

    http://www.ibm.com/developerworks/cn/opensource/os-cn-Red5/ Red5 是一个采用 Java 开发的开源免费 Flash 流媒体服务器.Red ...