BZOJ 3296: [USACO2011 Open] Learning Languages
Time Limit: 5 Sec Memory Limit: 128 MB
Submit: 387 Solved: 206
[Submit][Status][Discuss]
Description
农夫约翰的N(2 <= N<=10,000)头奶牛,编号为1.. N,一共会流利地使用M(1<= M <=30,000)种语言,编号从1
.. M.,第i头,会说K_i(1 <= K_i<= M)种语言,即L_i1, L_i2,…, L_{iK_i} (1 <= L_ij <= M)。 FJ的奶牛
不太聪明,所以K_i的总和至多为100,000。两头牛,不能直接交流,除非它们都会讲某一门语言。然而,没有共同
语言的奶牛们,可以让其它的牛给他们当翻译。换言之,牛A和B可以谈话,当且仅当存在一个序列奶牛T_1,T_2,
…,T_k,A和T_1都会说某一种语言,T_1和T_2也都会说某一种语言……,并且T_k和B会说某一种语言。农夫约翰
希望他的奶牛更加团结,所以他希望任意两头牛之间可以交流。他可以买书教他的奶牛任何语言。作为一个相当节
俭的农民,FJ想要购买最少的书籍,让所有他的奶牛互相可以说话。帮助他确定:*他必须购买的书籍的最低数量
Input
*第1行:两个用空格隔开的整数:N和M
*第2..N+1行:第i+1行描述的牛i的语言,K_i+1个空格隔开的整数:
K_iL_i1 L_i2,…,L_I{K_i}。
Output
*第1行:一个整数,FJ最少需要购买的书籍数量
Sample Input
3 3
2 3 2
1 2
1 1
Sample Output
1
//给三号牛买第二本书即可
解题思路
将每只牛和自己会说的语言连一条边,最后统计联通块的个数-1即为答案
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int MAXN = 200005;
inline int rd(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
while(isdigit(ch)) {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
int n,m;
int head[MAXN<<1],cnt;
int to[MAXN<<1],nxt[MAXN<<1];
vector<int> t[MAXN];
int col_num;
bool vis[MAXN<<1];
inline void add(int bg,int ed){
to[++cnt]=ed,nxt[cnt]=head[bg],head[bg]=cnt;
}
inline void dfs(int x){
vis[x]=1;
for(register int i=head[x];i;i=nxt[i]){
int u=to[i];
if(vis[u]) continue;
dfs(u);
}
}
int main(){
n=rd();m=rd();
for(register int i=1;i<=n;i++){
int x=rd();
for(register int j=1;j<=x;j++){
int y=rd();
add(i,y+10000);add(y+10000,i);
}
}
for(register int i=1;i<=n;i++)
if(!vis[i]) col_num++,dfs(i);
cout<<col_num-1<<endl;
return 0;
}
BZOJ 3296: [USACO2011 Open] Learning Languages的更多相关文章
- BZOJ 3296 [USACO2011 Open] Learning Languages:并查集
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3296 题意: 农夫约翰的N(2 <= N <= 10,000)头奶牛,编号为1 ...
- BZOJ——3296: [USACO2011 Open] Learning Languages
http://www.lydsy.com/JudgeOnline/problem.php?id=3296 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: ...
- 【BZOJ】3296: [USACO2011 Open] Learning Languages(tarjan)
http://www.lydsy.com/JudgeOnline/problem.php?id=3296 显然,每群能交流的群是个强联通块 然后求出scc的数量,答案就是scc-1 #include ...
- BZOJ3296: [USACO2011 Open] Learning Languages
3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 81 Solved: ...
- BZOJ3296: [USACO2011 Open] Learning Languages 并查集
Description 农夫约翰的N(2 <= N<=10,000)头奶牛,编号为1.. N,一共会流利地使用M(1<= M <=30,000)种语言,编号从1 .. M., ...
- BZOJ3296:Learning Languages(简单并查集)
3296: [USACO2011 Open] Learning Languages Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 436 Solved ...
- CodeForces 277A Learning Languages (并检查集合)
A. Learning Languages time limit per test:2 seconds memory limit per test:256 megabytes The "Be ...
- [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 ...
- C. Learning Languages 求联通块的个数
C. Learning Languages 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring&g ...
随机推荐
- PAT甲级——A1104 Sum of Number Segments
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...
- java最大余数法(百分比算法Echarts)
最近工作中使用Echarts开发报表的时候遇到了这样的一个问题,需求是一个div中左边是一个环形图表,右边是一个表格,表格中展示图表中每个类别占用的百分比.存在的问题:1.当存在四舍五入的时候,Ech ...
- Java(8)中List的遍历方式总结
本篇文章主要讲述了List这一集合类型在Java,包括Java8中的遍历方式,不包括其他的过滤,筛选等操作,这些操作将会在以后的文章中得到提现,由List可以类推到Set等类似集合的遍历方式. pub ...
- INI文件读写类
public class INIClass { public string inipath; [DllImport("kernel32")] private static exte ...
- 动态库加载时GetLasterror();值总是126的原因
1.dll路径不正确,导致找不到dll文件. 2.有可能是你要载入的DLL在内部还需要载入其它的dll,而它不存在,同样会返回126错误代码.比如一个你给系统添加了一个PCI设备,像AD采集卡之类的, ...
- [转]使用RDLC报表
使用RDLC报表(一) 1 建立数据源 启动VS2005新建一个窗体项目,命名为TestProj 在左边的窗体内选择“添加新数据源”或在菜单上操作“添加新数据源”: 选择后出现对话窗体,选 ...
- HZOI20190803 B题
题目:https://www.cnblogs.com/Juve/articles/11295333.html 话说这题方法挺多 40分:暴力 65:莫队,你会T得飞起 我考场上没打出带修莫队,没有修改 ...
- UOJ261 【NOIP2016】天天爱跑步 LCA+动态开点线段树
UOJ261 [NOIP2016]天天爱跑步 Description 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.天天爱跑步是一个养成类游戏,需要玩家每天按时上线, ...
- 转:linux select 多路复用机制
源地址:http://blog.csdn.net/turkeyzhou/article/details/8609360 2013-02-25 14:18 442人阅读 评论(1) 收藏 举报 目录 ...
- 【DM642】ICELL Interface—Cells as Algorithm Containers
ICELL Interface—Cells as Algorithm Containers: DSP的算法标准(XDAIS)为算法提供了一个标准的接口.这样我们就可以使用第三方的算法.For tech ...