Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 554 Solved: 346
[Submit][Status][Discuss]
Description
The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is grazing in one of N (1 <= N <= 1,000) pastures, conveniently numbered 1...N. The pastures are connected by M (1 <= M <= 10,000) one-way paths (no path connects a pasture to itself). The cows want to gather in the same pasture for their picnic, but (because of the one-way paths) some cows may only be able to get to some pastures. Help the cows out by figuring out how many pastures are reachable by all cows, and hence are possible picnic locations.
Input
* Line 1: Three space-separated integers, respectively: K, N, and M * Lines 2..K+1: Line i+1 contains a single integer (1..N) which is the number of the pasture in which cow i is grazing. * Lines K+2..M+K+1: Each line contains two space-separated integers, respectively A and B (both 1..N and A != B), representing a one-way path from pasture A to pasture B.
第1行输入K,N,M.接下来K行,每行一个整数表示一只奶牛所在的牧场编号.接下来M行,每行两个整数,表示一条有向路的起点和终点
Output
* Line 1: The single integer that is the number of pastures that are reachable by all cows via the one-way paths.
所有奶牛都可到达的牧场个数
Sample Input
2
3
1 2
1 4
2 3
3 4
INPUT DETAILS:
4<--3
^ ^
| |
| |
1-->2
The pastures are laid out as shown above, with cows in pastures 2 and 3.
Sample Output
牧场3,4是这样的牧场.
HINT
Source
题解:
深搜
从每个奶牛开始遍历即可。。。我还用了个bitset来加速。。。但数据范围小。。。没有用。。。233
注意有环!!!
#include<bits/stdc++.h>
using namespace std;
#define MAXN 1010
struct node
{
int begin,end,next;
}edge[];
int cnt,Head[MAXN],vis[MAXN],a[];
bitset<MAXN>vv;
void addedge(int bb,int ee)
{
edge[++cnt].begin=bb;edge[cnt].end=ee;edge[cnt].next=Head[bb];Head[bb]=cnt;
}
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
void dfs(int u)
{
int i,v;
vis[u]++;vv[u]=;
for(i=Head[u];i!=-;i=edge[i].next)if(vv[edge[i].end]==)dfs(edge[i].end);
}
int main()
{
int k,n,m,i,bb,ee,ans;
k=read();n=read();m=read();
for(i=;i<=k;i++)a[i]=read();
memset(Head,-,sizeof(Head));cnt=;
for(i=;i<=m;i++)
{
bb=read();ee=read();
addedge(bb,ee);
}
memset(vis,,sizeof(vis));
for(i=;i<=k;i++){vv.reset();dfs(a[i]);}
ans=;
for(i=;i<=n;i++)if(vis[i]==k)ans++;
printf("%d",ans);
return ;
}
Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset的更多相关文章
- BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )
直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...
- BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is graz ...
- bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐【dfs】
从每个奶牛所在草场dfs,把沿途dfs到的草场的con都+1,最后符合条件的草场就是con==k的,扫一遍统计一下即可 #include<iostream> #include<cst ...
- 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 432 Solved: 270[ ...
- 【BZOJ】1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐(dfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1648 水题.. dfs记录能到达的就行了.. #include <cstdio> #in ...
- bzoj1648 [Usaco2006 Dec]Cow Picnic 奶牛野餐
Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is graz ...
- BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )
有点类似背包 , 就是那样子搞... --------------------------------------------------------------------------------- ...
- Bzoj 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 传递闭包,bitset
1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 891 Solved: 590 ...
- BZOJ——1649: [Usaco2006 Dec]Cow Roller Coaster
http://www.lydsy.com/JudgeOnline/problem.php?id=1649 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 7 ...
随机推荐
- (转)iOS中3种正则表达式的使用与比较
.利用NSPredicate(谓词)匹配 例如匹配有效邮箱: NSString *email = @“nijino_saki@.com”: NSString *regex = @"[A-Z0 ...
- linux根目录详解
ubuntu 文件说明:http://tech.ccidnet.com/art/302/20080118/1347213_1.html/ 根目录 | |-boot/ 启动文件.所有与系统启动有关的 ...
- 使用原生js与jQuery分别实现一个简单的tab页签
tab页签通常适用于空间有限而内容较多同时兼顾页面美观度不给用户一种信息过量视觉疲劳的情形.使用面非常广,下面我们用两种方法简单实现之. 首先,构建页面元素.页签的可点击部分我们通常用列表来承载,包括 ...
- DelphiXE4- System.IOUtils.TDirectory笔记查询后缀名为dll的文件
TStringDynArray 在System.Types中定义
- VS2015 C#.net4.6 windows的定时服务
1.创建项目 C#->经典桌面->windows服务 2.创建定时服务 找到服务1,自动生成了 OnStart(服务开启时运行),OnStop(服务关闭时运行)两个函数,我们需要在开启时新 ...
- 修改UISearBar的文字颜色,placehoder颜色及输入框颜色
UISearchBar是我们经常会用到的一个控件-- 它由两个subView组成的,一个是UISearchBarBackGround,另一个是UITextField UITextField默认输入字体 ...
- UML 类图的关系
1. 关联关系 1.1 单向关联 . public class ClassA { private ClassB bVar; } public class ClassB { //... } 1.2 ...
- Uva 1050 Ars Longa
Description You have been struck with inspiration, and are designing a beautiful new art sculpture f ...
- 机器学习的数学基础(1)--Dirichlet分布
机器学习的数学基础(1)--Dirichlet分布 这一系列(机器学习的数学基础)主要包括目前学习过程中回过头复习的基础数学知识的总结. 基础知识:conjugate priors共轭先验 共轭先验是 ...
- phpstorm 强大的活动模板 可以自定义注释,代码段,根据cms订制自动提示
http://jingyan.baidu.com/article/8275fc86badd6346a03cf6aa.html [PHP] phpstorm的使用(1) http://v.youku.c ...