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.

K(1 \le K \le 100)K(1≤K≤100) 只奶牛分散在 N(1 \le N \le 1000)N(1≤N≤1000) 个牧场.现在她们要集中起来进餐。牧场之间有 M(1 \le M \le 10000)M(1≤M≤10000) 条有向路连接,而且不存在起点和终点相同的有向路.她们进餐的地点必须是所有奶牛都可到达的地方。那么,有多少这样的牧场可供进食呢?

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 1

Inputcopy Outputcopy
2 4 4
2
3
1 2
1 4
2 3
3 4
2

Hint

The cows can meet in pastures 3 or 4.

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;int k,n,m,bj[1005],a[1005],u,v,ans;
vector<int>adj[10010];
bool vis[1005];void dfs(int x){for(int y:adj[x]){if(!vis[y]){bj[y]++;vis[y]= true;dfs(y);}}
}
int main(){scanf("%d%d%d",&k,&n,&m);for(int i=1;i<=k;i++){scanf("%d",&a[i]);}for(int i=1;i<=m;i++){scanf("%d%d",&u,&v);adj[u].push_back(v);}for(int i=1;i<=k;i++){bj[a[i]]++;vis[a[i]]=1;dfs(a[i]);memset(vis,0,sizeof(vis));}for(int i=1;i<=n;i++){if(bj[i]==k)ans++;}printf("%d",ans);
}

G - Cow Picnic S相关推荐

  1. bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐(暴力DFS)

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 774  Solved: 480 ...

  2. Cow Picnic(POJ-3256)

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

  3. P2853 [USACO06DEC]Cow Picnic S

    题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...

  4. [USACO06DEC]牛的野餐Cow Picnic DFS

    题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...

  5. 洛谷 P2853 Cow Picnic S(DFS)

    https://www.luogu.com.cn/problem/P2853 题目大意 k头牛在n个点上,m条有向边,统计那些所有牛都能到达的点的个数 思路 很容易就想到,以每头牛为起点去深搜,能走到 ...

  6. 洛谷 P2853 [USACO06DEC]Cow Picnic S-dfs

    输入: 2 4 4 2 3 1 2 1 4 2 3 3 4 输出: 2 代码如下: #include <iostream> #include <vector> #include ...

  7. 2019牛客假日团队赛3 题解

    https://ac.nowcoder.com/acm/contest/945#question 题号 标题 已通过代码 题解/讨论 通过率 团队的状态 A 奶牛披萨 点击查看 进入讨论 42/71 ...

  8. c++ 遍历所有点且距离最短_L3图论第08课 图的遍历

    L3-图论-第08课 图的遍历 图的遍历是指,从给定图中任意指定的顶点(称为初始点)出发,按照某种搜索方法沿着图的边访问图中的所有顶点,使每个顶点仅被访问一次,这个过程称为图的遍历.遍历过程中得到的顶 ...

  9. ACM入门之【图论习题】

    目录 P5318 [深基18.例3]查找文献[★ 图的遍历] P3916 图的遍历[★★ 求每一个点可以到达的最大的点 反向建图] P1113 杂务[★ ★ 拓扑排序 求完成所有杂务所需的最短时间] ...

最新文章

  1. 在enable mmu之前可以使用mmu了
  2. 【LSB】图片隐写主体函数
  3. pythonpath manager_python 路径操作工具 pathlib,比 os 模块好用太多
  4. 从零打造一个程序员的mac
  5. xp计算机如何查看内存大小,xp如何查看内存大小
  6. html怎么设置光线,vray渲染器太阳光参数怎么设置?
  7. 第九届蓝桥杯JavaC组省赛真题
  8. 计算机硬盘不识别u盘启动,PE识别不了硬盘是怎么回事?
  9. 日常工作记录:AlertDialog中的取消、确定按钮显示不到问题
  10. Nginx反向正向代理
  11. lamp技术_LAMP技术简介:
  12. 艾利特代理商—艾利特EC63机器人安装教程
  13. 工具 svn 介绍和简单用法
  14. 截图工具(win+shift+S)截图之后为什么粘贴不上?有了这个方法,不用单独下载多次复制粘贴的工具了
  15. 双十二大家都在买哪些书?这份书单请码住
  16. 《用户故事与敏捷方法》读书笔记 02 细节是什么
  17. Xshell配置密钥公钥(Public key)与私钥(Private Key)登录
  18. [week2]每周总结与工作计划
  19. 亲手撸了一个SpringBoot+Vue的企业级项目(附源码)
  20. 图扑喜获第十一届中国创新创业大赛全国赛优秀奖!

热门文章

  1. 海天英雄传 java_登陆源码
  2. 关于C++ pair 和make_pair的用法
  3. Gephi丰富的插件一瞥
  4. 【负荷预测】基于蚂蚁优化算法的BP神经网络在负荷预测中的应用研究(Matlab完整代码实现)
  5. Postman请求https接口配置
  6. day03-线性回归、逻辑回归、岭回归
  7. Go实现算法:N的阶乘
  8. 浏览器通过apache访问php文件提示下载问题,更改项目文件目录
  9. iPhone已疯,继续大幅降价拉抬销量
  10. 【ASP.NET】——文件上传