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

Line 1: Three space-separated integers, respectively: KN, and M 
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

Line 1: The single integer that is the number of pastures that are reachable by all cows via the one-way paths.

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的更多相关文章

  1. Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 554  Solved: 346[ ...

  2. POJ 3045 Cow Acrobats (贪心)

    POJ 3045 Cow Acrobats 这是个贪心的题目,和网上的很多题解略有不同,我的贪心是从最下层开始,每次找到能使该层的牛的风险最小的方案, 记录风险值,上移一层,继续贪心. 最后从遍历每一 ...

  3. BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )

    直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...

  4. 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 432  Solved: 270[ ...

  5. poj 3348 Cow 凸包面积

    Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8122   Accepted: 3674 Description ...

  6. 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 ...

  7. bzoj1648 / P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 你愿意的话,可以写dj. 然鹅,对一个缺时间的退役选手来说,暴力模拟是一个不错的选择. 让每个奶牛都把图走一遍,显然那些被每个奶牛都走 ...

  8. POJ 3176 Cow Bowling(dp)

    POJ 3176 Cow Bowling 题目简化即为从一个三角形数列的顶端沿对角线走到底端,所取得的和最大值 7 * 3 8 * 8 1 0 * 2 7 4 4 * 4 5 2 6 5 该走法即为最 ...

  9. POJ 2184 Cow Exhibition【01背包+负数(经典)】

    POJ-2184 [题意]: 有n头牛,每头牛有自己的聪明值和幽默值,选出几头牛使得选出牛的聪明值总和大于0.幽默值总和大于0,求聪明值和幽默值总和相加最大为多少. [分析]:变种的01背包,可以把幽 ...

随机推荐

  1. 关于VMNet1、VMNet8、

    关于vmnet1~~~~~vmnet8 2008年04月11日 星期五 23:18 先说vmnet0,实际上就是一个虚拟的网桥,这个网桥有很若干个端口,一个端口用于连接你的Host,一个端口用于连接你 ...

  2. bzoj-1492 货币兑换Cash (1)——平衡树维护凸包

    题意: 有n天和m的初始金钱,用来购买AB两种纪念券: n天里每天都有AB的价格.每天能够进行这种操作. 1.卖出手中x%的纪念券(AB分别都卖出x%). 2.用x的金钱买入纪念券.买入AB券的比例在 ...

  3. CCControlExtension/CCControlPotentiometer

    #ifndef __CCCONTROLPOTENTIOMETER_H__ #define __CCCONTROLPOTENTIOMETER_H__ #include "CCControl.h ...

  4. X-射线:探索原子世界的利器

    请看下图: 上图是在现代量子力学意义上的氦(Helium)原子图像,原子像是一个黑乎乎的"小云团"(电子云包围着原子核).原子的半径大约仅仅有1埃. "埃"的符 ...

  5. 《UNIX环境高级编程》读书笔记之系统数据文件和信息(1)

    1.UNIX系统口令文件包括了下图所看到的的各字段,这些字段包括在<pwd.h>中定义的passwd结构体中 POSIX定义了两个获取口令文件项的函数. 在给出用户登录名或用户ID后.这两 ...

  6. bzoj1218: [HNOI2003]激光炸弹(DP二维前缀和)

    1218: [HNOI2003]激光炸弹 题目:传送门 题解: 一道经典题目啊... 为了更好的操作...把整个坐标系向右上角移动,从(1,1)开始 那么f[i][j]统计一下以(i,j)作为右上角, ...

  7. 为什么不针对internal接口写单元测试?

    测试驱动的开发(TDD,Test Driven Development)的核心理念,是要使得重构(refactoring)更为有效,而不是创建更多的测试. 对一个有着长生命周期的项目来讲,在它的第一个 ...

  8. Linux停止tomcat运行

    打开终端cd /java/tomcat#执行bin/startup.sh #启动tomcatbin/shutdown.sh #停止tomcattail -f logs/catalina.out #看t ...

  9. HDU-2204- Eddy’s爱好 (容斥原理)

    题意 给出一个数n,问1-n中有多少个数可以表示为m^k,m,k均为正整数且k>1 (1<=n<=1^18) 题解 (一开始^以为是异或懵逼了好久....) 额,显然1这个数比较讨厌 ...

  10. BZOJ 4033[HAOI2015] 树上染色(树形DP)

    4033: [HAOI2015]树上染色 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 3188  Solved: 1366[Submit][Stat ...