题目描述

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

输入输出格式

输入格式:

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.

输出格式:

Line 1: The single integer that is the number of pastures that are reachable by all cows via the one-way paths.

输入输出样例

输入样例#1: 复制

2 4 4
2
3
1 2
1 4
2 3
3 4

输出样例#1: 复制

2

说明

The cows can meet in pastures 3 or 4.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 260005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 98765431;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;inline int rd() {int x = 0;char c = getchar();bool f = false;while (!isdigit(c)) {if (c == '-') f = true;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f ? -x : x;
}ll gcd(ll a, ll b) {return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {if (!b) {x = 1; y = 0; return a;}ans = exgcd(b, a%b, x, y);ll t = x; x = y; y = t - a / b * y;return ans;
}
*/int n, m, K;
vector<int>vc[1002];
int cow[102];
int num[1002];
int vis[1002];void dfs(int u) {int siz = vc[u].size();num[u]++; vis[u] = 1;for (int i = 0; i < siz; i++) {int v = vc[u][i];if (!vis[v])dfs(v);}return;
}int main()
{// ios::sync_with_stdio(0);K = rd(); n = rd(); m = rd();for (int i = 1; i <= K; i++)cow[i] = rd();for (int i = 1; i <= m; i++) {int u = rd(), v = rd();vc[u].push_back(v);}for (int i = 1; i <= K; i++) {ms(vis);dfs(cow[i]);}int ans = 0;for (int i = 1; i <= n; i++) {//  cout << i << ' ' << num[i] << endl;if (num[i] == K)ans++;}cout << ans << endl;return 0;
}

转载于:https://www.cnblogs.com/zxyqzy/p/10371617.html

[USACO06DEC]牛的野餐Cow Picnic DFS相关推荐

  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. 信息学奥赛一本通 1343:【例4-2】牛的旅行 | 洛谷 P1522 [USACO2.4] 牛的旅行 Cow Tours

    [题目链接] ybt 1343:[例4-2]牛的旅行 洛谷 P1522 [USACO2.4] 牛的旅行 Cow Tours [题目考点] 1. 图论 最短路径 Floyd算法 Floyd算法时间复杂度 ...

  4. P3033 [USACO11NOV]牛的障碍Cow Steeplechase

    P3033 [USACO11NOV]牛的障碍Cow Steeplechase 题目描述 详见:P3033 [USACO11NOV]牛的障碍Cow Steeplechase solution 裸题. 对 ...

  5. 题解 洛谷 P3069 [USACO13JAN]牛的阵容Cow Lineup

    洛谷P3069[USACO13JAN]牛的阵容CowLineup\color{#00F}{洛谷\ P3069\ [USACO13JAN]牛的阵容Cow Lineup}洛谷 P3069 [USACO13 ...

  6. 脏牛(Dirty COW)漏洞攻击实验(SEED-Lab:Dirty-COW Attack Lab)

    <脏牛(Dirty COW)漏洞攻击实验> 目录 <脏牛(Dirty COW)漏洞攻击实验> **一:实验目的** **二:实验步骤与结果** **漏洞原理:** **COW机 ...

  7. 洛谷 P2853 Cow Picnic S(DFS)

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

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

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

最新文章

  1. [repost]Xcode因为证书问题经常报的那些错
  2. Spring boot集成mongodb
  3. 从入门到放弃,.net构建博客系统(二):依赖注入
  4. PHP验证登录用户名和密码
  5. uCOS中任务调度时的上下文切换
  6. bootstrap-表单控件——单选按钮水平排列
  7. 56. 合并区间 golang
  8. Android拍照上传代码样例
  9. cmmi写文档工作教训
  10. docker 镜像备份magento 2.2.3
  11. 人的大脑是如何识别运动物体
  12. 小功能大用处 ---- 一键检测各服务器是否存活
  13. js基础-17-解析url的函数,字符串出现的次数最多,并统计它出现几次
  14. iomega ix2 Android,Lenovo Iomega ix2系统安装_联想 IOMEGA IX2_服务器评测与技术-中关村在线...
  15. unbalanced calls to begin/end appearance transitions for uiviewcontroller的解决方法
  16. 小程序地图定位,打开地图导航,选择位置
  17. 使用Excel对国外B2B电商平台进行描述性数据分析
  18. python hook pc微信_一起来用python玩一波微信呀 | 防撤回, 好友分析, 聊天机器人~...
  19. 计算机英语课件模版,小学英语三年级下学期教学课件大纲模板
  20. 想学CNC编程的一定要看过来~

热门文章

  1. 高精度减法(C++实现)
  2. docker commit 发布自己的镜像
  3. 怎么把文件放在python目录下-python – 如何将文件下载到特定目录?
  4. 计算机系统结构广义定义,《计算机系统结构》电子教案(清华版).ppt.ppt
  5. SpringMVC框架的详细操作步骤和注解的用法
  6. 《深度探索C++对象模型》--4 Function语意学
  7. 【自动驾驶】欧拉角和旋转矩阵之间的转换
  8. 【自动驾驶】33.【图像坐标系】 到 【像素坐标系】 的度量单位变换、【英寸】、【感光芯片】
  9. 微博收藏(机器学习探讨)(二)
  10. 入门 Webpack,看这篇就够了