题目描述

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\).

题解

这是一道对图进行深度优先遍历的一道很好的练习题。

从题面中,我们可以知道,这道题目是让我们对每只奶牛所在的点进行深度优先遍历,找到遍历的次数正好等于奶牛数的点,最后输出这样的点的数量。

我们使用一个数组\(s[x]\)表示点\(x\)被遍历的次数,如果遍历到了点\(x\),那么\(s[x]\)就加\(1\)。

注意:每次遍历之前都需要将判断点是否已经访问过的\(vis[]\)数组清空,并且每次在遍历下一个点的时候都需要判断点是否已经访问,因为每一个点在每次遍历中都是最多访问\(1\)次。

另外,本题中\(n\)的范围并不大,因此我们可以使用邻接矩阵来存图。

不难得出\(AC\)代码。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>//头文件准备 using namespace std;//使用标准名字空间 //以下为快速读入
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar();}
return f * x;
} int n, m, k, ans, g[1003][1003], c[1003], s[1003], vis[1003];
//n,m,k的含义如题面,ans为最终答案,g数组为邻接矩阵,c数组存储牛的位置,s数组为每个点被遍历的次数,vis数组用来判断点是否已经被访问过 void dfs(int x)//进行图的深度优先遍历
{
vis[x] = 1;//将现在访问的点标记为已遍历
++s[x];//将这个点遍历的次数+1
for (int i = 1; i <= n; i++)//枚举节点编号
{
if (!vis[i] && g[x][i]) //如果当前节点没有被访问过并且与当前节点有边连接
dfs(i);//就遍历i号节点
}
} int main()
{
k = gi(), n = gi(), m = gi();//分别输入k,m,n(注意顺序)
for (int i = 1; i <= k; i++) c[i] = gi();//输入每只奶牛的顺序
for (int i = 1; i <= m; i++)
{
int u = gi(), v = gi(); //输入边两端的点的编号
g[u][v] = 1;//连接两边(注意不是双向边,是单向边)
}
for (int i = 1; i <= k; i++)//对奶牛的位置进行枚举
{
dfs(c[i]);//从每一只奶牛的位置开始遍历
memset(vis, 0, sizeof(vis));//记得每次遍历完都需要清空标记数组
}
for (int i = 1; i <= n; i++)
{
if (s[i] == k) ++ans;//统计答案,如果当前节点被访问的次数恰好为奶牛的只数
}
printf("%d\n", ans);//输出最后答案
return 0;//完美结束
}

完结撒花~

题解【洛谷P2853】[USACO06DEC]牛的野餐Cow Picnic的更多相关文章

  1. 洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  2. 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

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

  4. bzoj1648 / P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 你愿意的话,可以写dj. 然鹅,对一个缺时间的退役选手来说,暴力模拟是一个不错的选择. 让每个奶牛都把图走一遍,显然那些被每个奶牛都走 ...

  5. P2853 [USACO06DEC]牛的野餐Cow Picnic

    ------------------------- 长时间不写代码了,从学校中抽身出来真的不容易啊 ------------------------ 链接:Miku ----------------- ...

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

  7. 洛谷P2854 [USACO06DEC]牛的过山车Cow Roller Coaster

    P2854 [USACO06DEC]牛的过山车Cow Roller Coaster 题目描述 The cows are building a roller coaster! They want you ...

  8. 洛谷P3080 [USACO13MAR]牛跑The Cow Run

    P3080 [USACO13MAR]牛跑The Cow Run 题目描述 Farmer John has forgotten to repair a hole in the fence on his ...

  9. 洛谷 P2909 [USACO08OPEN]牛的车Cow Cars

    传送门 题目大意: m个车道. 如果第i头牛前面有k头牛,那么这头牛的最大速度会 变为原本的速度-k*D,如果速度小于l这头牛就不能行驶. 题解:贪心 让初始速度小的牛在前面 代码: #include ...

随机推荐

  1. 如何通过给MM修电脑培养感情

    文章来自网络 在修之前,向MM反复声明,这电脑故障是有硬件和软件之分的,如果是硬件故障,例如显卡风扇不转了,显示器连线老化,显示器分辨率超出显示器指标,等等都会导致黑屏啊,这个我不回家用专门的工具是修 ...

  2. AspxGridView 客户端点击获取对应的列值

    Html 内容: <dx:ASPxGridView ID="ASPxGridViewCluster" runat="server" Width=" ...

  3. 并查集-E - Wireless Network

    E - Wireless Network An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical t ...

  4. AE创建组件失败,项目中已存在对esri.arcgis.***的引用

    AE创建组件失败,项目中已存在对esri.arcgis.***的引用 解决办法:在解决方案资源管理器的引用中把错误提示中的引用删掉,再创建组件就没问题了.

  5. python接口自动化之发送get(三)

    1.安装requests requests是python的第三方库,需要进行安装.安装之前最好先关闭fiddler cmd(win+R快捷键)输入:pip install requests 其他命令: ...

  6. conn (php)

    <?php$host="localhost";$db_user="root"; //数据库用户$db_pass=""; //数据库密码 ...

  7. [BZOJ4310] 跳蚤 - 后缀数组,二分,ST表

    [BZOJ4310] 跳蚤 Description 首先,他会把串分成不超过 \(k\) 个子串,然后对于每个子串 \(S\) ,他会从 \(S\) 的所有子串中选择字典序最大的那一个,并在选出来的 ...

  8. 以POST方式发送

    URL url = null; String inputLine = null; HttpURLConnection httpurlconnection = null; try { //取上级电警平台 ...

  9. 参考 ZTree 加载大数据量。加载慢问题解析

    参考 ZTree 加载大数据量. 1.一次性加载大数据量加载说明 1).zTree v3.x 针对大数据量一次性加载进行了更深入的优化,实现了延迟加载功能,即不展开的节点不创建子节点的 DOM. 2) ...

  10. Microsonf visual c++ 14+ 离线内网安装

    内网离线安装方法:先下载官方的visualcppbuildtools: <br  href=http://go.microsoft.com/fwlink/?LinkId=691126 >& ...