POJ 3256 Cow Picnic
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 4928 | Accepted: 2019 |
Description
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
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
Sample Input
2 4 4
2
3
1 2
1 4
2 3
3 4
Sample Output
2 思路:DFS全图,记录每个牧场可以到达的牛的数量,若pa[v] == K,则所有牛可以到达。![]()
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 10005
using namespace std;
typedef struct{
int to, next;
}Node;
Node edge[MAX];
int head[], vis[], pa[MAX], in[MAX];
void dfs(int s){
for(int i = head[s];i != -;i = edge[i].next){
int v = edge[i].to;
if(!vis[v]){
pa[v] ++;
vis[v] = ;
dfs(v);
}
}
}
int main(){
int K, N, M, u, v;
/* freopen("in.c", "r", stdin); */
while(~scanf("%d%d%d", &K, &N, &M)){
memset(head, -, sizeof(head));
memset(pa, , sizeof(pa));
for(int i = ;i <= K;i ++){
scanf("%d", &in[i]);
pa[in[i]] ++;
}
for(int i = ;i <= M;i ++){
scanf("%d%d", &u, &v);
edge[i].to = v;
edge[i].next = head[u];
head[u] = i;
}
for(int i = ;i <= K;i ++){
memset(vis, , sizeof(vis));
vis[in[i]] = ;
dfs(in[i]);
}
int ans = ;
for(int i = ;i <= N;i ++)
if(pa[i] == K) ans ++;
cout << ans << endl;
}
return ;
}
POJ 3256 Cow Picnic的更多相关文章
- Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 554 Solved: 346[ ...
- POJ 3045 Cow Acrobats (贪心)
POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...
- BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )
直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...
- 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 432 Solved: 270[ ...
- poj 3348 Cow 凸包面积
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8122 Accepted: 3674 Description ...
- POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包)
POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包) Description N (1 ≤ N ...
- bzoj1648 / P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 你愿意的话,可以写dj. 然鹅,对一个缺时间的退役选手来说,暴力模拟是一个不错的选择. 让每个奶牛都把图走一遍,显然那些被每个奶牛都走 ...
- POJ 3176 Cow Bowling(dp)
POJ 3176 Cow Bowling 题目简化即为从一个三角形数列的顶端沿对角线走到底端,所取得的和最大值 7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5 该走法即为最 ...
- POJ 2184 Cow Exhibition【01背包+负数(经典)】
POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...
随机推荐
- HDU 1709
MB,一开始就想到是不是只要加上一个不选择砝码的情况,但一直没动手做,因为看了看网上了,觉得总有点复杂,认为自己想错了.... 相信自己 #include <iostream> #incl ...
- mysql的查询练习1
1.多表查询
- 【VC++游戏开发】智力游戏——鸡蛋里挑骨头(仿扫雷)
在我学习游戏开发的过程中,遇到的最大的麻烦就是不知道一个游戏的完整实现过程,代码倒是其次. 这里,总结一下我做过的游戏.主要是梳理整每一个步骤. 先看下终于的效果 第1步,准备素材图片 包含鸡蛋.骨头 ...
- poj2385(dp)
题目链接:http://poj.org/problem?id=2385 Apple Catching Time Limit: 1000MS Memory Limit: 65536K Total S ...
- hdu Escape
Escape 题目: 非常裸的多重匹配. 可是点数较多,所以要用到状态压缩. . .. .. 第一次写. 好厉害的赶脚. #include <iostream> #include < ...
- ORA-01733: virtual column not allowed here
基表: hr.tt scott.tt 视图1: 基于 hr.tt union all scott.tt ---> scott.ttt 视图2: 基于 视图1->scott.ttt ...
- nyoj--747--蚂蚁的难题(三)(dp背包)
蚂蚁的难题(三) 时间限制:2000 ms | 内存限制:65535 KB 难度:4 描述 蚂蚁终于把尽可能多的食材都搬回家了,现在开始了大厨计划. 已知一共有 n 件食材,每件食材有一个美味度 ...
- WPF学习(三) - 依赖属性
学习WPF时,我在看一本叫做“深入浅出WPF”的书.整整20页都在讲依赖性性和附加属性,反复看了几遍居然还是不懂,真是郁闷. 上一篇中WPF绑定的例子,其实已经用到了依赖属性. // 作为被绑定的目标 ...
- python 3.x 学习笔记9 (面向对象)
1.面向对象 面向对象是一种对现实世界理解和抽象的方法,是计算机编程技术发展到一定阶段后的产物. 2.类(class): 一个类即是对一类拥有相同属性的对象的抽象.蓝图.原型.在类中定义了这些对象的都 ...
- http请求post,返回excel文件,并接收
1.post的方法里要加responseType: 'arraybuffer'参数,不然下载的excel会乱码 2.使用{type: "application/vnd.ms-excel&qu ...