题意:FJ的k头牛在n个牧场吃草,任意两个牧场之间可能有一条单向路径联通,共有m条,希望求出有多少牧场有可能让所有的牛都能到达。
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.
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.
Sample Input
2 4 4 2 3 1 2 1 4 2 3 3 4
Sample Output
2
思路:由于数据大,选择邻接表存储图,dfs每个有牛的牧场,看别的牧场的牛能通过单向路径到达该牧场的数量,若数量为k,则计数++,遍历一遍后得到符合题意的牧场数。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
#define MAX 1050
int cow[105],visit[MAX],index[MAX];//分别为牛数量,访问标示,符合题意的计数;
int graph[MAX][MAX],neigh[MAX];//邻接表储存
int k,n,m;
void dfs(int pos)
{
visit[pos]=1;
index[pos]++;//计数++
for(int i=0;i<neigh[pos];i++)//遍历该牧场能联通的牧场
{
if(visit[graph[pos][i]]==0)
dfs(graph[pos][i]);
}
}
int main()
{
int i,j,a,b;
scanf("%d%d%d",&k,&n,&m);
for(i=0;i<k;i++)//输入
{
scanf("%d",&cow[i]);
}
memset(graph,0,sizeof(graph));
memset(neigh,0,sizeof(neigh));
memset(index,0,sizeof(index));
for(i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
graph[a][neigh[a]++]=b;//邻接表实现储存:与a相邻的点的依次排列
}
for(i=0;i<k;i++)
{
memset(visit,0,sizeof(visit));//每次都得重新初始化成未访问
dfs(cow[i]);
}
int ans=0;
for(i=1;i<=n;i++)
{
if(index[i]==k)//符合题意
ans++;
}
printf("%d\n",ans);
return 0;
}