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

时间: 2024-08-04 00:06:55

POJ 3256 Cow Picnic 搜索的相关文章

poj 2484 Cow Exhibition 【变形0-1背包】

题目:poj 2484 Cow Exhibition 题意:给出n头牛,每头牛有一个幸运值 si 和聪明值 ti ,现在要选出一些牛,让两个值的和最大,前提是sum(si)和sum(ti)都是非负值. 分析:此题数据量不大,可以暴搜+剪枝水过. 这里要说的是0-1背包的思想,这个题目明显的变形就是物品有两个属性值,而且都要选最大的. 那么我们可不可以把一个值固定下来来求另一个值的最大值,根据0-1背包的思想,定义状态:dp[i]表示装入一些物品使得sum(si)的时候最大的sum(ti)的值.

poj 1985 Cow Marathon 【树的直径】

题目:poj 1985 Cow Marathon 题意:给出一个树,让你求树的直径. 分析: 树的直径:树上两点之间的最大距离. 我们从任意一点出发,BFS一个最远距离,然后从这个点出发,在BFS一个最远距离,就是树的直径. AC代码: /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostream> #include <algorithm> #include

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include

POJ 3613 Cow Relays 恰好n步的最短路径

http://poj.org/problem?id=3613 题目大意: 有T条路,从s到e走n步,求最短路径. 思路: 看了别人的... 先看一下Floyd的核心思想: edge[i][j]=min(edge[i][j],edge[i][k]+edge[k][j]) i到j的最短路是i到j的直接路径或者经过k点的间接路径,但是矩阵的更新总是受到上一次更新的影响 如果每次的更新都存进新矩阵,那么edge[i][k]+edge[k][j]是不是表示只经过三个点两条边的路径呢? min(edge[i

poj 3270 Cow Sorting 置换群 简单题

假设初始状态为 a:2 3 1 5 4 6 则目标状态为 b:1 2 3 4 5 6且下标为初始状态中的3 1 2 4 5 6(a[3],a[1]...) 将置换群写成循环的形式 (2,3,1),(5,4),6就不用移动了. 移动方式2种 1:选循环内最小的数和其他len-1个数交换 2:选整个序列最小的数和循环内最小的数交换,转到1,再换回来. #include<cstdio> #include<queue> #include<algorithm> #include&

poj 3270 Cow Sorting(初涉置换群)

http://poj.org/problem?id=3270 大致题意:给出n个整数,要将它们转化成递增序列,每交换其中两个数的代价是这两个数之和.问排序成功后的最小代价. 该题考察的是置换群知识.在黑书p247上有详细的讲解.总结下置换群,方便复习. 群:给定一个集合G={a,b,c...}和集合G上的二元运算 ·,如果满足封闭性,结合律,存在单位元和逆元,则成集合G在运算'·'之下是一个群. 置换:n个元素1,2,....,n之间的置换可表示为  1     2     3     ...

poj 3348 Cow 凸包面积

Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8122   Accepted: 3674 Description Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are f

poj 1699 Best Sequence (搜索技巧 剪枝 dfs)

题目链接 题意:给出几个基因片段,要求你将它们排列成一个最短的序列,序列中使用了所有的基因片段,而且不能翻转基因. 分析:先计算出add数组,再dfs枚举. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio> 6 #include <vector> 7 #include <al

POJ 1946 Cow Cycling

Cow Cycling Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2516   Accepted: 1396 Description The cow bicycling team consists of N (1 <= N <= 20) cyclists. They wish to determine a race strategy which will get one of them across the fin