POJ 3256 Cow Picnic 搜索

题目大意:有一些奶牛在一些牧场里,这些牧场有些单向边,牧场中的牛按照单向边行走,问有哪些牧场所有牛都能到达。


思路:图的连通性本来应该是tarjan或者并查集什么的,但是这个题数据范围是在是太弱了,所以就搜索就行了。


CODE:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 10010
using namespace std;

int cows,points,edges;
bool have_cow[MAX];

int head[MAX],total;
int next[MAX],aim[MAX];
bool v[MAX];

int cnt[MAX],blocks;

inline void Add(int x,int y)
{
	next[++total] = head[x];
	aim[total] = y;
	head[x] = total;
}

void DFS(int x)
{
	if(v[x])	return ;
	cnt[x]++;
	v[x] = true;
	for(int i = head[x]; i; i = next[i])
		DFS(aim[i]);
}

int main()
{
	cin >> cows >> points >> edges;
	for(int x,i = 1; i <= cows; ++i) {
		scanf("%d",&x);
		if(!have_cow[x])
			have_cow[x] = true,++blocks;
	}
	for(int x,y,i = 1; i <= edges; ++i) {
		scanf("%d%d",&x,&y);
		Add(x,y);
	}
	for(int i = 1; i <= points; ++i)
		if(have_cow[i]) {
			memset(v,false,sizeof(v));
			DFS(i);
		}
	int ans = 0;
	for(int i = 1;i <= points; ++i)
		if(cnt[i] == blocks)	ans++;
	cout << ans << endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值