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. scss-@extend

    @extend指令用于共享规则和选择器之间的关系.它可以扩展所有其他类的样式在一个类中,也可应用于自己特定的样式. 查看如下scss@extend示例: .style{ font-size: 30px ...

  2. 使用CKRule实现PVC配方计算

    1,PVC计算的基本原理 配方员设计好配方,再进行抽象提炼,会出现相对于软件而言可以理解的逻辑,如属性的概念,对厂企生成的PVC产品而言,一般都有产品大类名称,花纹,颜色,长度,宽度,厚度等概念,这对 ...

  3. C/C++函数指针,指针函数的用法,用处

     先看函数指针 int func2(int x); /* 声明一个函数 */ int (*q2) (int x); /* 声明一个函数指针 */ q2=func2;    /* 将func函数的首地址 ...

  4. git 无法忽略Android Studio 生成的 .idea目录解决办法

    在Android Studio中导入了别的人Gradle项目,产生了 .idea文件夹, 然后git 发现了这个变动,修改了 .gitignore不起作用,仍然不能忽略这个文件夹 在项目目录里面 右键 ...

  5. 本地数据库(sql server)插入一条新数据时,同步到服务器数据库

    之前有个同学问我,本地数据库插入新数据时怎么同步到服务器上,当时我先想到是程序逻辑控制,作相应的处理. 但有时候我们程序不太好处理,那能不能从数据库入手呢,数据库不是有触发器(Trigger)吗,应该 ...

  6. Flask环境github及项目部署(十三)

    (1) github项目搭建 1 连接GitHub hostnamectl set-hostname flask-dev # 设置 hostname,一般是主机名可以不更改 ssh-keygen # ...

  7. 移动端调试工具weinre

    前段时间在大豹公众号上看到一个关于移动端调试的工具,了解了一下,确实不错. npm install -g weinre 先全局安装,然后使用的时候通过如下命令启动 weinre --boundHost ...

  8. March 12 2017 Week 11 Sunday

    I learned the value of hard work by working hard. 只有真的努力了,才会知道努力的价值. I know the value of hard work, ...

  9. github的初步认识

    第一部分:我用代码写了一个简单的可以运行的helloworld代码. 其链接为:https://github.com/ljw305503/CK01.git 第二部分:GitHub是一个分布式的版本控制 ...

  10. python入门8 输入输出

    输入 input() 输出 print() #coding:utf-8 #/usr/bin/python """ 2018-11-03 dinghanhua 输入输出 & ...