POJ3256:Cow Picnic
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 5432 | Accepted: 2243 |
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遍历.
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
bool mp[MAXN][MAXN];
int belong[MAXN];//记录每个cow所属的pasture
int gather[MAXN];//记录每个pasture所能聚集的cow的个数
int vis[MAXN];
int k,n,m;
void dfs(int u)
{
vis[u]=;
gather[u]++;
for(int i=;i<=n;i++)
{
if(mp[u][i]&&!vis[i])//存在环
{
dfs(i);
}
}
}
int main()
{
while(scanf("%d%d%d",&k,&n,&m)!=EOF)
{
memset(mp,false,sizeof(mp));
memset(belong,,sizeof(belong));
memset(gather,,sizeof(gather));
memset(vis,,sizeof(vis));
for(int i=;i<=k;i++)
{
int x;
scanf("%d",&x);
belong[i]=x;
}
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u][v]=true;
}
for(int i=;i<=k;i++)
{
memset(vis,,sizeof(vis));
dfs(belong[i]);
}
int res=;
for(int i=;i<=n;i++)
if(gather[i]==k) //pasture聚集的row数目为k则res+1
res++;
printf("%d\n",res);
}
return ;
}
Java:
import java.util.*; public class Main{
static Scanner cin = new Scanner(System.in);
static final int MAXN=1005;
static int k,n,m;
static int[] load=new int[105];
static ArrayList<Integer>[] arc=new ArrayList[MAXN];
static int[] mark=new int[MAXN];
static boolean[] vis=new boolean[MAXN];
static void dfs(int u)
{
mark[u]++;
vis[u]=true;
for(int i=0;i<arc[u].size();i++)
{
int to=arc[u].get(i);
if(!vis[to])
{
dfs(to);
}
}
}
public static void main(String[] args){ while(cin.hasNext())
{
Arrays.fill(load, 0);
Arrays.fill(mark, 0);
k=cin.nextInt();
n=cin.nextInt();
m=cin.nextInt();
for(int i=1;i<=n;i++)
{
arc[i]=new ArrayList<Integer>();
}
for(int i=1;i<=k;i++)
{
int pasture=cin.nextInt();
load[i]=pasture;
}
for(int i=0;i<m;i++)
{
int u,v;
u=cin.nextInt();
v=cin.nextInt();
arc[u].add(v);
}
for(int i=1;i<=k;i++)
{
if(load[i]!=0)
{
Arrays.fill(vis, false);
dfs(load[i]);
}
}
int res=0;
for(int i=1;i<=n;i++)
{
if(mark[i]==k)
res++;
}
System.out.println(res);
}
}
}
POJ3256: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[ ...
- 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[ ...
- bzoj1648 / P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 你愿意的话,可以写dj. 然鹅,对一个缺时间的退役选手来说,暴力模拟是一个不错的选择. 让每个奶牛都把图走一遍,显然那些被每个奶牛都走 ...
- 洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...
- 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...
- POJ 3256 Cow Picnic
Cow Picnic Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4928 Accepted: 2019 Descri ...
- 洛谷P2853 [USACO06DEC]牛的野餐Cow Picnic
题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...
- 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 ...
随机推荐
- Intellij IDEA打开多项目窗口
我版本是2016.02.04 其他版本可能不一样的设置
- ubuntu编译内核模块报错:Required key not available 的解决
系统为ubuntu18.04, 在编译内核模块insmod helloworld.ko的时候提示如下错误. 出现此问题的原因是,Ubuntu Kernel 使用 EFI_SECURE_BOOT_SIG ...
- SpringBoot学习笔记(2):引入Spring Security
SpringBoot学习笔记(2):用Spring Security来保护你的应用 快速开始 本指南将引导您完成使用受Spring Security保护的资源创建简单Web应用程序的过程. 参考资料: ...
- QCon2016 上海会议汇总(1) - 前端技术实践
日程传送门:http://2016.qconshanghai.com/schedule 我这里重点总结下前端.移动端.团队管理.研发支撑相关的议题,谈谈我的感受. <Vue 2.0: 渐进式前端 ...
- python 时间感觉能用到的
datetime, string, timestamp 互转 import time import datetime print datetime.datetime.now() print datet ...
- Redis高级进阶(二)
一.消息通知 在一些网站上,经常会有一些发布/订阅或者邮件订阅的功能,尤其一些博客上.其实这种问题很常见,当页面需要进行如发送邮件.复杂的计算时会阻塞页面的渲染.为了避免用户等待太久,应该使用其他进程 ...
- UI控制滑杆插件
在线演示 本地下载
- sass表达式前后出现空格
最近发现一个问题,当我用mixin将px转rem时: @mixin pxToRem($remVal: 1){ font-size: ($remVal/75)rem; }.a { @include px ...
- Linux查看外网IP
Linux查看外网IP curl cip.cc curl ifconfig.me curl ipinfo.io
- Spark- SparkStreaming可更新状态的实例
Producer package zx.zx.sparkkafka import java.util.Properties import kafka.producer.{KeyedMessage, P ...