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.

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

Hint

The cows can meet in pastures 3 or 4.
 
dfs题,搜索每只牛能到达的位置。
 
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 1000005
int prime[N];
int pn=0;
int vis[10005];
int st[10005];
int val[10005];
int a[10005];
int b[10005];
int h[10005];
int k,n,m;
void dfs(int x)
{
val[x]=1;vis[x]++;
for(int i=h[x];i;i=a[i])
{
if(!val[b[i]])
dfs(b[i]); }
}
int main()
{
//int k,n,m;
cin>>k>>n>>m;
int num=0;
for(int i=1;i<=k;i++)
{
cin>>st[i];
//vis[st[i]]++;
}
int x,y;
for(int i=1;i<=m;i++)
{
cin>>x>>y;
a[i]=h[x];
b[i]=y;
h[x]=i; }
for(int i=1;i<=k;i++)
{
memset(val,0,sizeof(val));
dfs(st[i]);
}
for(int i=1;i<=n;i++)
{
if(vis[i]==k)
num++;
}
cout<<num<<endl;
}

  

poj_3256_Cow Picnic的更多相关文章

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

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

  3. 【POJ 1639】 Picnic Planning (最小k度限制生成树)

    [题意] 有n个巨人要去Park聚会.巨人A和先到巨人B那里去,然后和巨人B一起去Park.B君是个土豪,他家的停车场很大,可以停很多车,但是Park的停车场是比较小.只能停k辆车.现在问你在这个限制 ...

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

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

  5. HDU 3045 Picnic Cows(斜率优化DP)

    Picnic Cows Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. bzoj1648 [Usaco2006 Dec]Cow Picnic 奶牛野餐

    Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is graz ...

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

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

  8. hdu 3045 Picnic Cows(斜率优化DP)

    题目链接:hdu 3045 Picnic Cows 题意: 有n个奶牛分别有对应的兴趣值,现在对奶牛分组,每组成员不少于t, 在每组中所有的成员兴趣值要减少到一致,问总共最少需要减少的兴趣值是多少. ...

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

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

随机推荐

  1. python搭建本地服务器

    python搭建本地服务器 python3以上版本 'python3 -m http.server 8000' 默认是8000端口,可以指定端口,打开浏览器输入http://127.0.0.1:800 ...

  2. rest-framework框架——解析器、ur控制、分页、响应器、渲染器、版本

    一.解析器(parser) 解析器在reqest.data取值的时候才执行. 对请求的数据进行解析:是针对请求体进行解析的.表示服务器可以解析的数据格式的种类. from rest_framework ...

  3. drupal 基础理论

    第3章 Drupal 的基本概念 添加新评论 浏览 6795 次 Drupal的基本概念主要包括节点.内容类型.模块.主题和分类等.只有对这些概念有了足够的了解,方能灵活的构建网站.本章将对这些基本概 ...

  4. pId的数据结构转children 数据结构(JS);

    在工作中经常遇到需要把带有pId的的list数据转换为children格式的树形结构,一直都没有找到太好的工具函数.偶然间看到了这个函数,研究了下,感觉这个函数很强大,所以记录下来,作为备用,同时也贴 ...

  5. Android应用开发详解

    目录结构 1.Android概述 2.Android开发基础 未完待续……

  6. Android学习——Fragment静态加载

    从今天开始做一套安卓的学习笔记,开发环境是Android Studio,把学习过程中的知识和遇到的问题都写在这里,先从Fragment开始学起. Fragment概述 Fragment是Android ...

  7. SharePoint中遇到Timeout

    使用SharePoint时会遇到不止一种的timeout(即超时)错误. 如果遇到了timeout, 该怎么区分呢? 大致上SharePoint可以控制和影响的timeout地方如下: 1. Shar ...

  8. solidity语言2

    变量类型(Value Types) # 布尔型 关键字 bool 值 true , false 操作符 !, &&, ||, ==, != # 整型 关键字 int(int256), ...

  9. Sql根据经纬度算出距离

    SELECT  ISNULL((2 * 6378.137 * ASIN(SQRT(POWER(SIN((117.223372- ISNULL(Latitude,0) )*PI()/360),2)+CO ...

  10. 数据结构与算法分析java——树2(二叉树类型)

    1. 二叉查找树 二叉查找树(Binary Search Tree)/  有序二叉树(ordered binary tree)/ 排序二叉树(sorted binary tree) 1). 若任意节点 ...