题目描述

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. 酸菜鱼的 DP动态规划 刷题记录

    BZOJ1026: [SCOI2009]windy数 数位dp.很多小细节... 代码: #include <bits/stdc++.h> using namespace std; typ ...

  2. many connection errors,更改max_connection_errors的值

    https://www.cnblogs.com/tonyccc/p/11496101.html https://blog.csdn.net/li_li_lin/article/details/7276 ...

  3. ActiveMQ使用JDBC持久化

    步骤一:创建一个数据库            步骤二:配置activemq.xml配置文件                1.在persistenceAdapter加入如下配置 <!--crea ...

  4. Angular项目目录

    0.模块介绍和基础知识 https://cloud.tencent.com/developer/section/1489514 1.如下图VSCode-- node_modules 第三方依赖包存放目 ...

  5. TCP的粘包和拆包问题及解决

    前言 TCP属于传输层的协议,传输层除了有TCP协议外还有UDP协议.那么UDP是否会发生粘包或拆包的现象呢?答案是不会.UDP是基于报文发送的,从UDP的帧结构可以看出,在UDP首部采用了16bit ...

  6. 1.spring异常:Caused by: java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springfr ...

  7. Win10激活工具 —— HWIDGen的使用方法

    一:引言 众所周知,Windows系统在安装完成之后,第一步就是激活系统,网上的一些激活方法大多数都是KMS激活,激活持续时间为半年. 因此,我找到了一个可以永久激活的工具:HWIDGen,它可以数字 ...

  8. Manacher算法求最长回文串模板

    #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> ...

  9. Linux - Shell - 字符串截取

    概述 简述 字符串 截取 背景 之前因为要给文件 批量重命名, 做过字符串截取 当时做好了, 也说了要写点东西 结果忘了 现在又要尝试批量 重命名 才发现之前的东西已经忘了好多 要是当时把博客写下来, ...

  10. pytorch save model + Gmatch4py + jupyter debugger + community structure

    1. pytorch, 使用训练好的模型测试自己图片 2. [ pytorch ] ——基本使用:(2) 训练好的模型参数的保存以及调用 3. Gmatch4py 4. Network Analysi ...