题目描述

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;
}

[USACO06DEC]牛的野餐Cow Picnic DFS的更多相关文章

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

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

  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

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

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

  5. 题解【洛谷P2853】[USACO06DEC]牛的野餐Cow Picnic

    题目描述 The cows are having a picnic! Each of Farmer John's \(K (1 ≤ K ≤ 100)\) cows is grazing in one ...

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

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

  7. BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )

    直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...

  8. 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 432  Solved: 270[ ...

  9. Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 554  Solved: 346[ ...

随机推荐

  1. 6410中的PWM&nbsp;定时器

    看了OK6410的手册,感觉晕晕的. 需要整理一下思路. 我觉得主要的知道下面这几个内容吧. 1. 定时器的电路结构. 2. 定时器的工作原理是什么.定时器如何来使用.{使用的时序是什么,在时间轴上各 ...

  2. Pacemaker实现双机热备

    在互联网高速发展的今天,尤其在电子商务的发展,要求服务器能够提供不间断服务.在电子商务中,如果服务器宕机,造成的损失是不可估量的.要保证服务器不间断服务,就需要对服务器实现冗余.在众多的实现服务器冗余 ...

  3. C# 堆和栈的区别?

    解释1.栈是编译期间就分配好的内存空间,因此你的代码中必须就栈的大小有明确的定义:堆是程序运行期间动态分配的内存空间,你可以根据程序的运行情况确定要分配的堆内存的大小 解释2. 存放在栈中时要管存储顺 ...

  4. 卡尔曼滤波总结——KF、EFK、UKF

    1.用途 现实是我们的处理和测量模型都是非线性的,结果就是一个不规则分布,KF能够使用的前提就是所处理的状态是满足高斯分布的,为了解决这个问题,EKF是寻找一个线性函数来近似这个非线性函数,而UKF就 ...

  5. 在Sqlserver中使用Try Catch

      创建错误日志表: CREATE TABLE ErrorLog(errNum INT,ErrSev NVARCHAR(1000),ErrState INT,ErrProc NVARCHAR(1000 ...

  6. 408. Valid Word Abbreviation有效的单词缩写

    [抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...

  7. hibernate 一对多(级联关系)

    hibernate 核心配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hiber ...

  8. Monkey测试异常信息解读

    查看包名 1.cmd 下面输入 adb locat > D:\test.txt 2.ctrl+c 停掉刚刚 1 运行的进程 3.打开test.txt文件--搜索  Displayed  对应的内 ...

  9. Appium移动端自动化测试之应用操作详解(四)

    应用操作篇 1.1).安装应用 desired_caps = { 'platformName': 'Android', 'platformVersion': '5.0.0.0', 'deviceNam ...

  10. redis的一些简介

    Redis是Remote Dictionary Server的缩写,他本质上一个Key/Value数据库,与Memcached类似的NoSQL型数据库. 1.       redis的数据类型: st ...