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

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 554  Solved: 346
[Submit][Status][Discuss]

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.

  K(1≤K≤100)只奶牛分散在N(1≤N≤1000)个牧场.现在她们要集中起来进餐.牧场之间有M(1≤M≤10000)条有向路连接,而且不存在起点和终点相同的有向路.她们进餐的地点必须是所有奶牛都可到达的地方.那么,有多少这样的牧场呢?

Input

* Line 1: Three space-separated integers, respectively: K, N, 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.

第1行输入K,N,M.接下来K行,每行一个整数表示一只奶牛所在的牧场编号.接下来M行,每行两个整数,表示一条有向路的起点和终点

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

INPUT DETAILS:

4<--3
^ ^
| |
| |
1-->2

The pastures are laid out as shown above, with cows in pastures 2 and 3.

Sample Output

2

牧场3,4是这样的牧场.

HINT

 

Source

Silver

题解:

深搜

从每个奶牛开始遍历即可。。。我还用了个bitset来加速。。。但数据范围小。。。没有用。。。233

注意有环!!!

 #include<bits/stdc++.h>
using namespace std;
#define MAXN 1010
struct node
{
int begin,end,next;
}edge[];
int cnt,Head[MAXN],vis[MAXN],a[];
bitset<MAXN>vv;
void addedge(int bb,int ee)
{
edge[++cnt].begin=bb;edge[cnt].end=ee;edge[cnt].next=Head[bb];Head[bb]=cnt;
}
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
void dfs(int u)
{
int i,v;
vis[u]++;vv[u]=;
for(i=Head[u];i!=-;i=edge[i].next)if(vv[edge[i].end]==)dfs(edge[i].end);
}
int main()
{
int k,n,m,i,bb,ee,ans;
k=read();n=read();m=read();
for(i=;i<=k;i++)a[i]=read();
memset(Head,-,sizeof(Head));cnt=;
for(i=;i<=m;i++)
{
bb=read();ee=read();
addedge(bb,ee);
}
memset(vis,,sizeof(vis));
for(i=;i<=k;i++){vv.reset();dfs(a[i]);}
ans=;
for(i=;i<=n;i++)if(vis[i]==k)ans++;
printf("%d",ans);
return ;
}

Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset的更多相关文章

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

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

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

    Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is graz ...

  3. bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐【dfs】

    从每个奶牛所在草场dfs,把沿途dfs到的草场的con都+1,最后符合条件的草场就是con==k的,扫一遍统计一下即可 #include<iostream> #include<cst ...

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

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

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

    http://www.lydsy.com/JudgeOnline/problem.php?id=1648 水题.. dfs记录能到达的就行了.. #include <cstdio> #in ...

  6. bzoj1648 [Usaco2006 Dec]Cow Picnic 奶牛野餐

    Description The cows are having a picnic! Each of Farmer John's K (1 <= K <= 100) cows is graz ...

  7. BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )

    有点类似背包 , 就是那样子搞... --------------------------------------------------------------------------------- ...

  8. Bzoj 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 传递闭包,bitset

    1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 891  Solved: 590 ...

  9. BZOJ——1649: [Usaco2006 Dec]Cow Roller Coaster

    http://www.lydsy.com/JudgeOnline/problem.php?id=1649 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 7 ...

随机推荐

  1. 【BZOJ3270】【高斯消元】博物馆

    Description 有一天Petya和他的朋友Vasya在进行他们众多旅行中的一次旅行,他们决定去参观一座城堡博物馆.这座博物馆有着特别的样式.它包含由m条走廊连接的n间房间,并且满足可以从任何一 ...

  2. 【DP_树形DP专题】题单总结

    转载自 http://blog.csdn.net/woshi250hua/article/details/7644959#t2 题单:http://vjudge.net/contest/123963# ...

  3. JavaWeb用户登录功能的实现

    大四快毕业了,3年多的时间中,乱七八糟得学了一大堆,想趁找工作之前把所学的东西整理一遍,所以就尝试着做一个完整的JavaWeb系统,这几天试着做了一个用户登录的功能,分享给大家,肯定有很多不完善的地方 ...

  4. javascript获取url参数的方法

    发布:thatboy   来源:Net     [大 中 小] 本文介绍下,在javascript中取得url中某一个参数的方法,这里分享一个小例子,供大家学习参考下.本文转自:http://www. ...

  5. (转载)在状态栏即时显示Hint

    在状态栏即时显示Hint(所有窗口,包括子窗口控件的hint) 在主窗体中放入一个ApplicationEvents控件.添加如下代码.其中Items[***]中的***代表状态栏的各个子panel, ...

  6. [简历] JAVA 软件工程师

    首先,一份好的简历不光说明事实,更通过FAB模式来增强其说服力. Feature:是什么 Advantage:比别人好在哪些地方 Benefit:如果雇佣你,招聘方会得到什么好处 其次,写简历和写议论 ...

  7. C# 使用Salt+Hash来为密码加密

    (一) 为什么要用哈希函数来加密密码 如果你需要保存密码(比如网站用户的密码),你要考虑如何保护这些密码数据,象下面那样直接将密码写入数据库中是极不安全的,因为任何可以打开数据库的人,都将可以直接看到 ...

  8. ios开发之C语言第3天

    变量的命名规则以及规范 变量的命名规则  1>变量名只能由任意的字母,下划线和$以及数字组成,注意不能用数字开头 2>区分大小写 3>变量一定要先定义再使用 4>同一个大括号中 ...

  9. html5应用程序标签

    一.html5应用程序标签 (1)datalist需要数据载体 input list属性指向数据源 <input type="text" list="input_l ...

  10. IOS webview中cookie的读取与保存-b

    Cookie 的读取 将它放在 webViewDidFinishLoad 开始后执行 NSArray *nCookies = [[NSHTTPCookieStorage sharedHTTPCooki ...