题目描述

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.

思路:从奶牛所在牧场出发,dfs遍历所有牧场,当牧场被遍历的次数==奶牛数时,表明所有奶牛都可到达此牧场,满足条件,ans++

代码如下:
#include<bits/stdc++.h>
using namespace std;
bool vis[1010];
int bian[1010],a[1010];
int k,n,m,ans;
vector<int> G[1010];
void dfs(int x){
    vis[x]=1;
    bian[x]++;//此牧场被遍历的次数
    for(int i=0;i<G[x].size();i++){
        if(!vis[G[x][i]]){
            dfs(G[x][i]);
        }
    }
}
int main(){
    cin>>k>>n>>m;
    for(int i=1;i<=k;i++){
        cin>>a[i];//每头奶牛所在牧场
    }
    for(int i=1;i<=m;i++){
        int x,y;
        cin>>x>>y;
        G[x].push_back(y);//建边
    }
    for(int i=1;i<=k;i++){
        memset(vis,0,sizeof(vis)); //记住每次遍历前都要先清0
        dfs(a[i]);
    }
    for(int i=1;i<=n;i++){
        if(bian[i]==k) ans++;//key!
    }
    cout<<ans<<endl;
    return 0;
}

P2853 [USACO06DEC]Cow Picnic S相关推荐

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

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

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

  3. [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 ...

  4. 洛谷 P2853 Cow Picnic S(DFS)

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

  5. Cow Picnic(POJ-3256)

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

  6. G - Cow Picnic S

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

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

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

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

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

  9. 洛谷题单【数据结构1-4】图的基本应用

    P5318 [深基18.例3]查找文献 思路 bfs和dfs的模板题 实现 #include<bits/stdc++.h> using namespace std; int n,m; ve ...

最新文章

  1. 字节跳动这份面试题,你能打几分
  2. MySQL Processlist--常见线程状态
  3. [bzoj2467][中山市选2010]生成树_快速幂
  4. 170304 地铁修建 ccf
  5. (非原)如何让.net开发的Winform程序快速释放内存
  6. 医生c语言测试卷b卷的答案,合肥工业大学C语言期中测试题_B卷
  7. 16 张图解带你掌握一致性哈希算法
  8. 用计算机坑人,10大坑人专业,坑你没商量!
  9. php导入qq数据txt代码,/谁有能都实现将excel文件导入到数据中,并在php网页上显示的源码啊,有的发送1091932879@qq.com,谢谢!...
  10. 初解vue脚手架vue-cli,及demo示例(一)
  11. 写在通用权限管理系统销售200套,从刚开始求人家用到人家主动索取,写一下亲身感受...
  12. C#中的委托和Java中的“委托”(Java8 lambda表达式)
  13. 【个人笔记】OpenCV4 C++ 快速入门 14课
  14. 20. jQuery 遍历 - 祖先
  15. 如何给硬盘分1T整数的空间
  16. 内存管理机制和垃圾回收机制
  17. word中插入一页横向页面
  18. Idea社区版部署项目到tomcat
  19. 0002深度学习初体验-基于Tensorflow and Keras 实现卷积神经网络(CNN-AlexNET)实现CIFAR图像训练
  20. Date setMonth(date.getMonth - 1)

热门文章

  1. 机器学习方法的PPT
  2. 当心这些“美”出来的病 [美联致美医学美容专家]
  3. 绘画教程:日系二次元女生头发画法
  4. 虚拟化运维中:为什么对网络流量监控这么重要?
  5. 为什么学编程的人大多数都去了深圳和北京?
  6. 下载素材资源的网站有 哪些?
  7. 赫墨拉游戏-服务端-编程规范
  8. 初学者必读VRay 2.0光源设置(2)——使用平面光源
  9. Realsense D435i关闭IR结构光
  10. 【web素材】05—13款炫酷的HTML5实例动画