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的更多相关文章

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

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3296 题意: 农夫约翰的N(2 <= N <= 10,000)头奶牛,编号为1 ...

  2. BZOJ——3296: [USACO2011 Open] Learning Languages

    http://www.lydsy.com/JudgeOnline/problem.php?id=3296 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit:  ...

  3. 【BZOJ】3296: [USACO2011 Open] Learning Languages(tarjan)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3296 显然,每群能交流的群是个强联通块 然后求出scc的数量,答案就是scc-1 #include ...

  4. BZOJ3296: [USACO2011 Open] Learning Languages

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

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

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

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

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

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

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

  8. [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 ...

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

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

随机推荐

  1. css3中 百分比宽度减去固定宽度的写法

    div{ /*实现了宽度为父容器宽度减去固定的300像素*/ width:-webkit-calc(100% - 300px); width:-moz-calc(100% - 300px); widt ...

  2. PAT甲级——A1070 Mooncake

    Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...

  3. 数据库insert和update

    1.当使用insert时不能使用where id=?,这是要使用update语句 2.只对一些列插入数据或者更新数据: insert是: insert tb(column1,column2..)val ...

  4. js 引入Vue.js实现vue效果

    拆分组件为单个js见:https://www.jianshu.com/p/2f0335818ceb 效果 html <!DOCTYPE html> <html> <hea ...

  5. Python-爬虫实战 简单爬取豆瓣top250电影保存到本地

    爬虫原理 发送数据 获取数据 解析数据 保存数据 requests请求库 res = requests.get(url="目标网站地址") 获取二进制流方法:res.content ...

  6. crontab定时任务语法及应用

    https://mp.weixin.qq.com/s/Oi9hppNQMeFiQo9s-ge79A crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows ...

  7. linux学习(四)-----linux常用指令

    touch 指令 touch 指令创建空文件 基本语法 touch 文件名称 应用实例 案例 1: 创建一个空文件 hello.txt cp 指令 cp 指令拷贝文件到指定目录 基本语法 cp [选项 ...

  8. Configuring to Debug and Workaround Broken Client Applications

    背景:C3P0数据库连接池占满 Configuring to Debug and Workaround Broken Client Applications http://www.mchange.co ...

  9. <init>与<clinit>的区别

    在编译生成class文件时,会自动产生两个方法,一个是类的初始化方法<clinit>, 另一个是实例的初始化方法<init> <clinit>:在jvm第一次加载c ...

  10. C++stl中vector的几种常用构造方法

    #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #i ...