标签:++ lines ble ati scan oss mes 存储 sample
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.
Input
Output
Sample Input
2 4 4 2 3 1 2 1 4 2 3 3 4
Sample Output
2
Hint
#include<cstdio> #include<vector> #define N 10001 using namespace std; int k,n,m; int cow[N]; int sum[N]; int vis[N]; vector<int> g[N]; void dfs(int i) { vis[i]=1;//标记为已到达 int len=g[i].size();//从当前牧场可到达其他牧场的个数 for(int j=0;j<len;j++)//枚举所有可到达的牧场 { int next=g[i][j]; if(!vis[next])//如果下一个牧场未到达,继续向下搜索 dfs(next); } } int main() { scanf("%d%d%d",&k,&n,&m); for(int i=1;i<=k;i++) scanf("%d",&cow[i]); while(m--)//vector邻接表建图 { int x,y; scanf("%d%d",&x,&y); g[x].push_back(y); } for(int i=1;i<=k;i++) { dfs(cow[i]);//dfs每一头牛 for(int j=1;j<=n;j++)//统计每一个牧场的到达数 { sum[j]+=vis[j]; vis[j]=0; } } int cnt=0; for(int i=1;i<=n;i++)//枚举所有牧场 if(sum[i]==k)//如果该牧场所有牛均能到达 cnt++; printf("%d\n",cnt); return 0; }
标签:++ lines ble ati scan oss mes 存储 sample
原文地址:https://www.cnblogs.com/xyqxyq/p/9432650.html