BZOJ3296: [USACO2011 Open] Learning Languages
3296: [USACO2011 Open] Learning Languages
Time Limit: 5 Sec Memory Limit: 128 MB
Submit: 81 Solved: 39
[Submit][Status]
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_i L_i1
L_i2,...,L_I{K_i}。
Output
*第1行:一个整数,FJ最少需要购买的书籍数量。
Sample Input
2 3 2
1 2
1 1
Sample Output
HINT
给三号牛买第二本书即可
Source
- #include<cstdio>
- #include<cstdlib>
- #include<cmath>
- #include<cstring>
- #include<algorithm>
- #include<iostream>
- #include<vector>
- #include<map>
- #include<set>
- #include<queue>
- #include<string>
- #define inf 1000000000
- #define maxn 40000+100
- #define maxm 500+100
- #define eps 1e-10
- #define ll long long
- #define pa pair<int,int>
- #define for0(i,n) for(int i=0;i<=(n);i++)
- #define for1(i,n) for(int i=1;i<=(n);i++)
- #define for2(i,x,y) for(int i=(x);i<=(y);i++)
- #define for3(i,x,y) for(int i=(x);i>=(y);i--)
- #define mod 1000000007
- using namespace std;
- inline int read()
- {
- int x=,f=;char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
- while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
- return x*f;
- }
- int n,m,ans=,s[maxn],fa[maxn];
- inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
- int main()
- {
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- n=read();m=read();
- for1(i,n+m)fa[i]=i;
- for1(i,n)
- {
- int x=read();
- for1(j,x)
- {
- int y=read(),xx=find(i),yy=find(y+n);
- if(xx!=yy)fa[yy]=xx;
- }
- }
- for1(i,n)s[find(i)]++;
- for1(i,n)if(s[i])ans++;
- printf("%d\n",ans-);
- return ;
- }
BZOJ3296: [USACO2011 Open] Learning Languages的更多相关文章
- BZOJ3296: [USACO2011 Open] Learning Languages 并查集
Description 农夫约翰的N(2 <= N<=10,000)头奶牛,编号为1.. N,一共会流利地使用M(1<= M <=30,000)种语言,编号从1 .. M., ...
- 【BZOJ】3296: [USACO2011 Open] Learning Languages(tarjan)
http://www.lydsy.com/JudgeOnline/problem.php?id=3296 显然,每群能交流的群是个强联通块 然后求出scc的数量,答案就是scc-1 #include ...
- 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
Time Limit: 5 Sec Memory Limit: 128 MB Submit: 387 Solved: 206 [Submit][Status][Discuss] Description ...
- 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 ...
随机推荐
- 组合数学第一发 hdu 2451 Simple Addition Expression
hdu 2451 Simple Addition Expression Problem Description A luxury yacht with 100 passengers on board ...
- Unity3D AssentStore 下载的package存放目录(WinXP,Win8,Mac OS X)
网上找到如下: Windows 8,C:\Users\<你的用户名>\AppData\Roaming\Unity\Asset Store Mac OS X,~/Library/Unity/ ...
- Mac下phpstorm 浏览器出现 502 bad gateway 解决办法
问题: 在网上搜了好久,都没有合适的解决办法,于是决定分享下自己鼓捣好久解决了的办法,希望能帮到大家. 出现上述情况,一般在phpstorm里都会出现这个提示 我们只需要点击蓝色的进去,或者点phps ...
- [Angular 2] Using a Reducer to Change an Object's Property Inside an Array
Reducers are also often used for changing a single property inside of other reducers. This lesson sh ...
- ViewPage 一次滑动多页
效果 size是3的倍数时 其他情况下 初始化 private List<HonerUserBean> anchorList; private ViewPager vp ...
- codevs 1028 花店橱窗布置 (KM)
/*裸地KM*/ #include<iostream> #include<cstdio> #include<cstring> #define maxn 110 #d ...
- svg text文字居中
<text x="100" y="100" text-anchor="middle" dominant-baseline=" ...
- 各种乱码,编码问题设置方法整理(UTF-8)
一.tomcat中文乱码问题 打开tomcat安装目录,在conf文件夹中找到server.xml文件 ,找到 <Connector port="8009" protoc ...
- MongoDB与PHP的添加、修改、查询、删除
链接数据库使用下面的代码创建一个数据库链接 <?php$connection = new Mongo(); //链接到 localhost:27017$connection = new Mong ...
- java: cannot execute binary file
转自:http://jxwpx.blog.51cto.com/15242/222572 java: cannot execute binary file 如果遇到这个错,一般是操作系统位数出问题了. ...