Codeforces 278C Learning Languages(并查集)
题意抽象出来就是求联通块的个数吧,然后添加最少边使图联通。
注意所有人都不会任何语言的时候,答案是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(并查集)的更多相关文章
- BZOJ3296: [USACO2011 Open] Learning Languages 并查集
Description 农夫约翰的N(2 <= N<=10,000)头奶牛,编号为1.. N,一共会流利地使用M(1<= M <=30,000)种语言,编号从1 .. M., ...
- CodeForces 277A Learning Languages (并检查集合)
A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes The "Be ...
- Codeforces Gym 100463E Spies 并查集
Spies Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Desc ...
- Codeforces 859E Desk Disorder 并查集找环,乘法原理
题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...
- Codeforces - 828C String Reconstruction —— 并查集find()函数
题目链接:http://codeforces.com/contest/828/problem/C C. String Reconstruction time limit per test 2 seco ...
- Codeforces 571D - Campus(并查集+线段树+DFS 序,hot tea)
Codeforces 题目传送门 & 洛谷题目传送门 看到集合的合并,可以本能地想到并查集. 不过这题的操作与传统意义上的并查集不太一样,传统意义上的并查集一般是用来判断连通性的,而此题还需支 ...
- CodeForces 455C Civilization (并查集+树的直径)
Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...
- Codeforces 650C Table Compression (并查集)
题意:M×N的矩阵 让你保持每行每列的大小对应关系不变,将矩阵重写,重写后的最大值最小. 思路:离散化思想+并查集,详见代码 好题! #include <iostream> #includ ...
- Codeforces 468B Two Sets 并查集
题目大意:给出n个数,要求将n个数分配到两个集合中,集合0中的元素x,要求A-x也再0中,同理1集合. 写了几个版本号,一直WA在第8组数据...最后參考下ans,写了并查集过了 学到:1.注意离散的 ...
随机推荐
- 如何合并IP网段
1. 安装IPy pip3 install IPy 2. 写脚本: yuyue@workplace:~ $ cat combine_ip.pyfrom IPy import IPSet, IPimpo ...
- C++重要知识点小结---1
1.C++中类与结构的唯一区别是:类(class)定义中默认情况下的成员是private的,而结构(struct)定义中默认情况下的成员是public的. 2. ::叫作用域区分符,指明一个函数属于哪 ...
- IOCP模型
IOCP http://blog.csdn.net/zhongguoren666/article/details/7386592 Winsock IO模型之IOCP模型 http://blog.csd ...
- bug报告-常用词汇中英对照表
- 一幅图概括Android测试的方方面面
一幅图概括Android测试的方方面面,来自网络: 另外的一些测试技巧 1,测试应用程序时,环境是很大的一个影响因素:系统时间,网络情况,异常关闭等 2,测试应用程序时,第三方嵌入程序也是有影响的.如 ...
- JSP中字符编码转换问题
问题描述:一个input.jsp页面中的参数,传递到另外一个save.jsp页面上,然后存入到数据库中,如果input.jsp页面输入偶数中文没有问题,输入奇数则出现?,存入数据库的也是?. 问题源码 ...
- DateTime.ToString格式化日期,使用DateDiff方法获取日期时间的间隔数
一:DateTime.ToString格式化日期 二:代码 using System; using System.Collections.Generic; using System.Component ...
- bzoj 1798 [Ahoi2009]Seq 维护序列seq(线段树+传标)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1798 [题意] 给定一个序列,要求提供区间乘/加,以及区间求和的操作 [思路] 线段树 ...
- Linux下gcc和g++编译helloworld
linux C(hello world) 1.使用vi/vim进行编写代码并保存为hello_world.c.如下: 1 2 3 4 5 6 /* This is my first C program ...
- 最简单例子图解JVM内存分配和回收
一.简介 JVM采用分代垃圾回收.在JVM的内存空间中把堆空间分为年老代和年轻代.将大量(据说是90%以上)创建了没多久就会消亡的对象存储在年轻代,而年老代中存放生命周期长久的实例对象.年轻代中又被分 ...