博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
阅读量:5347 次
发布时间:2019-06-15

本文共 2303 字,大约阅读时间需要 7 分钟。

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.

  K(1≤K≤100)只奶牛分散在N(1≤N≤1000)个牧场.现在她们要集中起来进餐.牧场之间有M(1≤M≤10000)条有向路连接,而且不存在起点和终点相同的有向路.她们进餐的地点必须是所有奶牛都可到达的地方.那么,有多少这样的牧场呢?

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.

所有奶牛都可到达的牧场个数

题解:

定义f[i][j],从i牛开始是否能到j这个点。

用dfs或bfs模拟每个点,然后对于点j,看是否每个i,f[i][j]都为真,则这个点可以选,统计个数即可。

代码:

#include
#include
#include
#include
#include
#include
//by zrt//problem:using namespace std;typedef long long LL;const int inf(0x3f3f3f3f);const double eps(1e-9);int k,n,m;int H[1005],tot,P[10005],X[10005];bitset<1005> sum[105];inline void add(int x,int y){ P[++tot]=y;X[tot]=H[x];H[x]=tot;}bool is[1005];void dfs(int x,int a){ sum[a][x]=1; for(int i=H[x];i;i=X[i]) if(!sum[a][P[i]]) dfs(P[i],a);}int num[105];int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif scanf("%d%d%d",&k,&n,&m); for(int i=1;i<=k;i++){ scanf("%d",&num[i]); } for(int i=1,x,y;i<=m;i++){ scanf("%d%d",&x,&y); add(x,y); } for(int i=1;i<=k;i++){ dfs(num[i],i); } int ans=0; for(int i=1;i<=n;i++){ bool ok=1; for(int j=1;j<=k;j++){ if(!sum[j][i]) { ok=0;break; } } if(ok) ans++; } printf("%d\n",ans); return 0;}

转载于:https://www.cnblogs.com/zrts/p/bzoj1648.html

你可能感兴趣的文章
直播技术细节3
查看>>
《分布式服务架构:原理、设计于实战》总结
查看>>
java中new一个对象和对象=null有什么区别
查看>>
字母和数字键的键码值(keyCode)
查看>>
IE8调用window.open导出EXCEL文件题目
查看>>
Spring mvc初学
查看>>
有意思的代码片段
查看>>
C8051开发环境
查看>>
VTKMY 3.3 VS 2010 Configuration 配置
查看>>
01_1_准备ibatis环境
查看>>
windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决
查看>>
JavaScript中的BOM和DOM
查看>>
360浏览器兼容模式 不能$.post (不是a 连接 onclick的问题!!)
查看>>
spring注入Properties
查看>>
【BZOJ-1055】玩具取名 区间DP
查看>>
Bit Twiddling Hacks
查看>>
LeetCode : Reverse Vowels of a String
查看>>
时间戳与日期的相互转换
查看>>
jmeter(五)创建web测试计划
查看>>
python基本数据类型
查看>>