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 ...
随机推荐
- Go 学习笔记
官网: https://golang.org/ 环境: $GOROOT: GOROOT环境变量指定了Go的安装目录. $GOPATH: GOPATH 环境变量指定workspace的目录. 命令行: ...
- centos6.9下设置nginx服务开机自动启动
首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令: vi /etc/init.d/nginx 在脚本中添加如下命令: #!/bin/sh # # nginx - ...
- Linux里的发消息
1.给指定用户发送消息 语法: write 用户名 说明: 1.用户是在线的 2.执行过程 a.敲完命令按回车,进入写信模式 b.写信的时候如果写错了Ctrl+退格 删除字符 c.写完以后Ctrl+D ...
- Openstack 架构简述
概述 在学习OpenStack的过程中,感觉对整个OpenStack的架构稍稍有些了解,所以将这些记录下来,一来防止自己忘记,二来也可以对有需要的人提供帮助 本文章相关的灵感/说明/图片来自于http ...
- python读取文件存到excel中
用xlwt模块执行代码报下面的错 ValueError: column index (256) not an int in range(256) xlwt 模块看源码说最大列只支持255列,所以超过这 ...
- Linux中cp命令不提示直接覆盖的方法
新做了服务器,cp覆盖时,无论加什么参数-f之类的还是提示是否覆盖,这在大量cp覆盖操作的时候是不能忍受的. 把a目录下的文件复制到b目录 cp –r a/* b 执行上面的命令时,b存在的每个文件都 ...
- Effective java -- 1
写博客我也不知道是不是一个好习惯,但是目前还不知道有什么其他更有效率的学习方法.现在的学习方法:看书,写博客.如果看明白一个东西,去写博客的话,这通常是一个浪费时间的行为,但是这个过程同样帮助自己二次 ...
- [GUI] QT事件与X11的关系
做了一段时间linux下与QT事件相关的工作,经常会遇到X11,总是苦于无法完全理解其与linux以及QT事件之间的关系,所以用两篇文章来简单总结下linux中的图形管理和QT事件与X11的关系. & ...
- 手机端适配rem代码片段
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 操作系统原理2——OS结构
操作系统原理2——OS结构 计算机系统是由硬件系统和软件系统两部分组成, 操作系统是软件系统的一个组成部分,它是直接在硬件系统的基础上工作的,所以在研究操作系统之前,先必须对计算机系统的结构有一个 ...