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背包,可以把幽 ...
随机推荐
- spark源代码action系列-foreach与foreachPartition
RDD.foreachPartition/foreach的操作 在这个action的操作中: 这两个action主要用于对每一个partition中的iterator时行迭代的处理.通过用户传入的fu ...
- ACdream 1127(Base Station-树状数组-2个约束条件)
Base Station Time Limit: 20000/10000MS (Java/Others)Memory Limit: 512000/256000KB (Java/Others) Subm ...
- C++语言笔记系列之十八——虚函数(1)
1.C++中的多态 (1)多态性:同一个函数的调用能够进行不同的操作,函数重载是实现多态的一种手段. (2)联编:在编译阶段进行联接.即是在编译阶段将一个函数的调用点和函数的定义点联接起来. A.静态 ...
- UVA 1016 - Silly Sort 置换分解 贪心
Silly Sort Your younger brother has an assignment and needs s ...
- ApacheFlink简介
对无界数据集的连续处理 在我们详细介绍Flink之前,让我们从更高的层面上回顾处理数据时可能遇到的数据集的类型以及您可以选择处理的执行模型的类型.这两个想法经常被混淆,清楚地区分它们是有用的. 首先, ...
- 高阶函数-lambda表达式
#2.6 map()# 第一个参数传入一个函数,,第二个参数为一个可迭代对象li_1 = (1,3,5,7)def funcA(x): return x*xm1 = map(funcA,li_1)pr ...
- php获取js里的参数
php获取js的值有如下方式: 1.php echo出js文件得到返回值,在gamemap.js文件中输出参数. echo '<script type="text/javascript ...
- 《Unix环境高级编程》读书笔记 第4章-文件和目录
1. stat结构的基本形式: on error 24. 设备特殊文件 每个文件系统所在的存储设备都由其主.次设备号表示. 设备号所用的数据类型是基本系统数据类型dev_t. 主设备号标识设备驱动程序 ...
- 路飞学城Python-Day12(practise)
# 函数基础# 1.写函数,计算传入数字参数的和(动态传参)# def sum_num(x,y):# return x+y# print(sum_num(1,2))# 2.写函数,用户传入修改的文件名 ...
- 设置fixed,横向滚动条失效
window.onscroll = function(){ var sl = -Math.max(document.body.scrollLeft,document.documentElement.s ...