POJ3256:Cow Picnic
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5432 | Accepted: 2243 |
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
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.
Output
Sample Input
2 4 4
2
3
1 2
1 4
2 3
3 4
Sample Output
2
思路:直接dfs遍历.
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
bool mp[MAXN][MAXN];
int belong[MAXN];//记录每个cow所属的pasture
int gather[MAXN];//记录每个pasture所能聚集的cow的个数
int vis[MAXN];
int k,n,m;
void dfs(int u)
{
vis[u]=;
gather[u]++;
for(int i=;i<=n;i++)
{
if(mp[u][i]&&!vis[i])//存在环
{
dfs(i);
}
}
}
int main()
{
while(scanf("%d%d%d",&k,&n,&m)!=EOF)
{
memset(mp,false,sizeof(mp));
memset(belong,,sizeof(belong));
memset(gather,,sizeof(gather));
memset(vis,,sizeof(vis));
for(int i=;i<=k;i++)
{
int x;
scanf("%d",&x);
belong[i]=x;
}
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u][v]=true;
}
for(int i=;i<=k;i++)
{
memset(vis,,sizeof(vis));
dfs(belong[i]);
}
int res=;
for(int i=;i<=n;i++)
if(gather[i]==k) //pasture聚集的row数目为k则res+1
res++;
printf("%d\n",res);
}
return ;
}
Java:
import java.util.*; public class Main{
static Scanner cin = new Scanner(System.in);
static final int MAXN=1005;
static int k,n,m;
static int[] load=new int[105];
static ArrayList<Integer>[] arc=new ArrayList[MAXN];
static int[] mark=new int[MAXN];
static boolean[] vis=new boolean[MAXN];
static void dfs(int u)
{
mark[u]++;
vis[u]=true;
for(int i=0;i<arc[u].size();i++)
{
int to=arc[u].get(i);
if(!vis[to])
{
dfs(to);
}
}
}
public static void main(String[] args){ while(cin.hasNext())
{
Arrays.fill(load, 0);
Arrays.fill(mark, 0);
k=cin.nextInt();
n=cin.nextInt();
m=cin.nextInt();
for(int i=1;i<=n;i++)
{
arc[i]=new ArrayList<Integer>();
}
for(int i=1;i<=k;i++)
{
int pasture=cin.nextInt();
load[i]=pasture;
}
for(int i=0;i<m;i++)
{
int u,v;
u=cin.nextInt();
v=cin.nextInt();
arc[u].add(v);
}
for(int i=1;i<=k;i++)
{
if(load[i]!=0)
{
Arrays.fill(vis, false);
dfs(load[i]);
}
}
int res=0;
for(int i=1;i<=n;i++)
{
if(mark[i]==k)
res++;
}
System.out.println(res);
}
}
}
POJ3256:Cow Picnic的更多相关文章
- Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 554 Solved: 346[ ...
- BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )
直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...
- 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 432 Solved: 270[ ...
- bzoj1648 / P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 你愿意的话,可以写dj. 然鹅,对一个缺时间的退役选手来说,暴力模拟是一个不错的选择. 让每个奶牛都把图走一遍,显然那些被每个奶牛都走 ...
- 洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...
- 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...
- POJ 3256 Cow Picnic
Cow Picnic Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4928 Accepted: 2019 Descri ...
- 洛谷P2853 [USACO06DEC]牛的野餐Cow Picnic
题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...
- 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 ...
随机推荐
- 页游手游服务器(二)c支持mysql
上一篇说的是liua的net拓展,这一篇说lua的sql拓展,准确说是mysql拓展,这里推荐下postgre,比mysql好用,支持数组,各种好,不过腾讯平台不支持,所以你的公司要和腾讯合作,掂量下 ...
- [IOI2018]组合动作
IOI2018 组合动作 UOJ 首先显然可以两次试出首字母 考虑增量构造 假设首字母为A,且已经试出前i个字母得到的串s 我们考虑press这样一个串s+BB+s+BX+s+BY+s+XA 首先这个 ...
- 我的Android进阶之旅------>如何为ListView组件加上快速滑块以及修改快速滑块图像
使用布局文件需要将android:fastScrollEnabled="true" ,如下代码所示: <ListView android:id="@+id/list ...
- ABAP动态生成经典应用之Dynamic SQL Excute 程序
[转自http://blog.csdn.net/mysingle/article/details/678598]开发说明:在SAP的系统维护过程中,有时我们需要修改一些Table中的数据,可是很多Ta ...
- 云计算服务的三种类型(SaaS、PaaS、IaaS)
云计算可以帮助企业降低IT方面的成本和复杂性,并获得他们蓬勃发展所需的灵活性与敏捷性.但是,规划出通往云的明确路径并非易事.毕竟用户需要看透与云相关的市场大肆宣传,然后理解并分析不同种类的云计算模式的 ...
- memset使用
void memset(void s, int ch, size_t n); 函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回 ...
- LintCode:链表操作(合并与反转)
描述: (1)翻转一个链表 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null ********************** ...
- Data Structure Linked List: Merge Sort for Linked Lists
http://www.geeksforgeeks.org/merge-sort-for-linked-list/ #include <iostream> #include <vect ...
- python中filter()函数
filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filt ...
- SpringCloud-断路器(Hystrix)
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用Rest Template + Ribbon和Feign来调用.为了保证其高可用,单 ...