1. 程式人生 > >[USACO06DEC]牛的野餐Cow Picnic DFS

[USACO06DEC]牛的野餐Cow Picnic DFS

copy turn may con cau 題目 math having std

題目描述

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