# |BZOJ 1648|DFS|[Usaco2006 Dec]Cow Picnic 奶牛野餐

BZOJ 1648
Luogu 2853
from: USACO 2006 Dec Sliver(USACO刷题第8题)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
using namespace std;

const int MAXN = 1000 + 5;

vector<int> G[MAXN];
int cs[MAXN], vi[MAXN], tot[MAXN], k, n, m;

void dfs(int u, int s) {
tot[u] += s;
for (int i=0;i<G[u].size();i++) {
int v = G[u][i];
if (!vi[v]) {
vi[v] = true;
dfs(v, s);
}
}
}
void clear() {
ms(tot, 0);
ms(cs, 0);
}
void init() {
clear();
for (int i=1;i<=k;i++) {
int ki;
scanf("%d", &ki);
cs[ki]++;
}
for (int i=1;i<=m;i++) {
int a, b;
scanf("%d%d", &a, &b);
G[a].push_back(b);
}
}
void solve() {
for (int i=1;i<=n;i++) {
if (cs[i]) {
ms(vi, 0), vi[i] = true;
dfs(i, cs[i]);
}
}
int ans = 0;
for (int i=1;i<=n;i++) if (tot[i]==k) ans++;
printf("%d\n", ans);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);freopen("1.out", "w", stdout);
#endif
while (scanf("%d%d%d", &k, &n, &m)==3&&k&&n&&m) init(), solve();
return 0;
}
• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

NotFound1

¥2 ¥4 ¥6 ¥10 ¥20