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背包,可以把幽 ...
随机推荐
- C++中对字符串进行插入、替换、删除操作
#include <iostream> #include <string> using std::cout; using std::endl; using std::strin ...
- 同一个TextView设置不同的颜色和大小
//strategy1是一个TextView SpannableStringBuilder builder1 = new SpannableStringBuilder(strategy1.getTex ...
- JavaSE入门学习24:Java面向对象补充
一Java中的Object类 Object类是全部Java类的父类.假设一个类没有使用extendskeyword明白标识继承另外一个类,那么这个类默认 继承Object类. public class ...
- HDU 5616 Jam's balance 背包DP
Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell ...
- chrome的F12的inspect使用
chrome中查看cookie https://stackoverflow.com/questions/10014996/how-do-you-check-cookies-using-chrome T ...
- m_Orchestrate learning system---九、在无法保证是否有图片的情况下,如何保证页面格式
m_Orchestrate learning system---九.在无法保证是否有图片的情况下,如何保证页面格式 一.总结 一句话总结:都配上默认缩略图就可以解决了 1.如何获取页面get方式传过来 ...
- ES线程池设置
每个Elasticsearch节点内部都维护着多个线程池,如index.search.get.bulk等,用户可以修改线程池的类型和大小,线程池默认大小跟CPU逻辑一致 一.查看当前线程组状态 cur ...
- BZOJ 2005 容斥原理
思路: 题目让求的是 Σgcd(i,j) (i<=n,j<=m) n,m不同 没法线性筛 怎么办? 容斥原理!! f[x]表示gcd(i,j)=x的个数 g[x]为 存在公约数=x 的数对 ...
- Eclipse里Tomcat报错:Document base ……does not exist or is not a readable directory(图文详解)
问题描述: 严重: Error starting static Resourcesjava.lang.IllegalArgumentException: Document base D:\Code\M ...
- html5+css3+javascript 自定义提示窗口
效果图: 源码: 1.demo.jsp <%@ page contentType="text/html;charset=UTF-8" language="java& ...