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

Line 1: Three space-separated integers, respectively: KN, 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.

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

  1. Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 554  Solved: 346[ ...

  2. BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )

    直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...

  3. 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 432  Solved: 270[ ...

  4. bzoj1648 / P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 你愿意的话,可以写dj. 然鹅,对一个缺时间的退役选手来说,暴力模拟是一个不错的选择. 让每个奶牛都把图走一遍,显然那些被每个奶牛都走 ...

  5. 洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  6. 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  7. POJ 3256 Cow Picnic

    Cow Picnic Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4928   Accepted: 2019 Descri ...

  8. 洛谷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 ...

  9. 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 ...

随机推荐

  1. python3 批量缩放图片为iphone5的640*1136以下

    try: from PIL import Image, ImageDraw, ImageFont, ImageEnhance except ImportError: import Image, Ima ...

  2. mysql主从复制(windows下)

    简单搭建mysql主从服务器(双向复制),仅供学习之用. 1.下载mysql-advanced-5.6.16-winx64.zip 下载完成后解压,重命名成mysql,分别放入两台服务器(看第二点集群 ...

  3. Ionic background地址写法问题

    1.背景图片 background:url(‘/img/text.jpg') 这种写法在手机上不好使 ’../img/text.jpg' 这种在手机上好使

  4. JS实现下拉列表的二级联动

    这个是简单也是最基本的下拉框联动的示例,这个示例主要针对那些只有二级联动,且第一级是固定的选项,第二级的内容也比较简单,不刷新的联动,动态的联动需要检索数据库,这个对不需要更新的二级联动比较实用.这里 ...

  5. JVM - 堆外内存

    看了不少资料,总结下: 堆外内存 / 直接内存(Direct Memory)JDK1.4中引入的NIO类,基于channel和Buffer的I/O方式,可用Native库直接分配堆外内存,然后利用一个 ...

  6. vue双向绑定补充说明方法

    本文总结自: https://segmentfault.com/a/1190000006599500,将每一个流程提炼出来做一个简单的说明,以免自己被繁杂的逻辑弄昏头脑~ observer: 遍历数据 ...

  7. python内置方法补充bin

    bin(x) 英文说明:Convert an integer number to a binary string. The result is a valid Python expression. I ...

  8. NOIP前的一些计划

    一些想法 距离NOIP2018只剩下一个星期的时间了,通过这几天在长郡的考试,渐渐感觉还有好多东西自己还不够熟练,也有些东西到现在还不会,现将NOIP前的一些计划列在这里,希望能在考前把他们全部完成吧 ...

  9. CentOS下查看MySQL的安装路径

    Linux下查看mysql.apache是否安装,并卸载. 指令 ps -ef|grep mysql 得出结果 root     17659     1  0  2011 ?        00:00 ...

  10. matlab中的科学记数法变成小数形式

    例如:假如rectx的形式在命令窗口显示: rectx = 1.0e+05 * 5.2294 5.2294 5.2294 5.2294 5.2294 那么,命令窗口输入vpa(rectx): ans ...