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

#### Description

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)条有向路连接，而且不存在起点和终点相同的有向路．她们进餐的地点必须是所有奶牛都可到达的地方．那么，有多少这样的牧场呢？

#### Input

* 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.

#### Output

* Line 1: The single integer that is the number of pastures that are reachable by all cows via the one-way paths.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<bitset>
//by zrt
//problem:
using namespace std;
typedef long long LL;
const int inf(0x3f3f3f3f);
const double eps(1e-9);
int k,n,m;
int H,tot,P,X;
bitset<1005> sum;
inline void add(int x,int y){
P[++tot]=y;X[tot]=H[x];H[x]=tot;
}
bool is;
void dfs(int x,int a){
sum[a][x]=1;
for(int i=H[x];i;i=X[i]) if(!sum[a][P[i]]) dfs(P[i],a);
}
int num;
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
scanf("%d%d%d",&k,&n,&m);
for(int i=1;i<=k;i++){
scanf("%d",&num[i]);
}
for(int i=1,x,y;i<=m;i++){
scanf("%d%d",&x,&y);
add(x,y);
}
for(int i=1;i<=k;i++){
dfs(num[i],i);
}
int ans=0;
for(int i=1;i<=n;i++){
bool ok=1;
for(int j=1;j<=k;j++){
if(!sum[j][i]) {
ok=0;break;
}
}
if(ok) ans++;
}
printf("%d\n",ans);
return 0;
}
posted @ 2014-09-12 06:06  zrt  阅读(291)  评论(0编辑  收藏  举报

Never stop,never ending!