G - Cow Picnic S
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相关推荐
- bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐(暴力DFS)
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 774 Solved: 480 ...
- Cow Picnic(POJ-3256)
Problem Description The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is graz ...
- 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 ...
- [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 ...
- 洛谷 P2853 Cow Picnic S(DFS)
https://www.luogu.com.cn/problem/P2853 题目大意 k头牛在n个点上,m条有向边,统计那些所有牛都能到达的点的个数 思路 很容易就想到,以每头牛为起点去深搜,能走到 ...
- 洛谷 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 ...
- 2019牛客假日团队赛3 题解
https://ac.nowcoder.com/acm/contest/945#question 题号 标题 已通过代码 题解/讨论 通过率 团队的状态 A 奶牛披萨 点击查看 进入讨论 42/71 ...
- c++ 遍历所有点且距离最短_L3图论第08课 图的遍历
L3-图论-第08课 图的遍历 图的遍历是指,从给定图中任意指定的顶点(称为初始点)出发,按照某种搜索方法沿着图的边访问图中的所有顶点,使每个顶点仅被访问一次,这个过程称为图的遍历.遍历过程中得到的顶 ...
- ACM入门之【图论习题】
目录 P5318 [深基18.例3]查找文献[★ 图的遍历] P3916 图的遍历[★★ 求每一个点可以到达的最大的点 反向建图] P1113 杂务[★ ★ 拓扑排序 求完成所有杂务所需的最短时间] ...
最新文章
- 在enable mmu之前可以使用mmu了
- 【LSB】图片隐写主体函数
- pythonpath manager_python 路径操作工具 pathlib,比 os 模块好用太多
- 从零打造一个程序员的mac
- xp计算机如何查看内存大小,xp如何查看内存大小
- html怎么设置光线,vray渲染器太阳光参数怎么设置?
- 第九届蓝桥杯JavaC组省赛真题
- 计算机硬盘不识别u盘启动,PE识别不了硬盘是怎么回事?
- 日常工作记录:AlertDialog中的取消、确定按钮显示不到问题
- Nginx反向正向代理
- lamp技术_LAMP技术简介:
- 艾利特代理商—艾利特EC63机器人安装教程
- 工具 svn 介绍和简单用法
- 截图工具(win+shift+S)截图之后为什么粘贴不上?有了这个方法,不用单独下载多次复制粘贴的工具了
- 双十二大家都在买哪些书?这份书单请码住
- 《用户故事与敏捷方法》读书笔记 02 细节是什么
- Xshell配置密钥公钥(Public key)与私钥(Private Key)登录
- [week2]每周总结与工作计划
- 亲手撸了一个SpringBoot+Vue的企业级项目(附源码)
- 图扑喜获第十一届中国创新创业大赛全国赛优秀奖!