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. Colorbox - a jQuery lightbox

    http://www.jacklmoore.com/colorbox/http://www.jacklmoore.com/colorbox/example5/ <script type=&quo ...

  3. BZOJ4010: [HNOI2015]菜肴制作(拓扑排序 贪心)

    题意 题目链接 Sol 震惊,HNOI竟出NOI原题 直接在反图上贪心一下. // luogu-judger-enable-o2 // luogu-judger-enable-o2 #include& ...

  4. html笔记【重新学习】

    <a></a>超链接标签: a标签有个target属性,<a href="#" target = "_self">在当前选项 ...

  5. HTML标签 随笔2

    3-1  使用ul   添加新闻信息列表 ul-li是没有前后顺序的信息列表. 语法: <ul> <li>信息</li> <li>信息</li&g ...

  6. 集合之equals与hashCode方法

    一  equals equals方法是Object级的,默认对比两个对象的内存地址,很多类都重写了该方法,对比对象的实际内容,一般对比同一类对象相同属性的属性值是否相同. 二 hashCode 1.哈 ...

  7. linux下查看磁盘分区的文件系统格式

    df -T 只可以查看已经挂载的分区和文件系统类型. Filesystem Type 1K-blocks Used Available Use% Mounted on/dev/sda1 ext4 20 ...

  8. java web的安全约束--表单的验证

    例子,表单和JDBCRealm的安全验证 参考了一篇文章http://www.cnblogs.com/dyllove98/archive/2013/07/31/3228698.html 1.要在wab ...

  9. HTML基础内容(持续更新...)

    1.<!DOCTYPE html>声明有助于浏览器中正确显示网页 HTML5<!DOCTYPE html>HTML 4.01<!DOCTYPE HTML PUBLIC & ...

  10. xshell里的一些配色方案

    只需要把下面的代码复制到记事本中,保存为xcs文件格式,然后在xshell中导入选中就可以换配色方案了,可以自己修改下面的代码 [ubuntu]text(bold)=ffffffmagenta(bol ...