Poj 3256 Cow Picnic

题意:FJ的k头牛在n个牧场吃草,任意两个牧场之间可能有一条单向路径联通,共有m条,希望求出有多少牧场有可能让所有的牛都能到达。

Input

Line 1: Three space-separated integers, respectively:  KN, 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.

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值