洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic
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 (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)条有向路连接,而且不存在起点和终点相同的有向路.她们进餐的地点必须是所有奶牛都可到达的地方.那么,有多少这样的牧场呢?
输入输出格式
输入格式:
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.
输出格式:
Line 1: The single integer that is the number of pastures that are reachable by all cows via the one-way paths.
输入输出样例
2 4 4 2 3 1 2 1 4 2 3 3 4
2 我可怜的dfs、、、50分,剩下的点全T了、、
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 11000
using namespace std;
bool vis[N],vist[N];
int k,n,m,x,y,ans,tot,a[N],sum[N],head[N];
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
struct Edge
{
int to,next,from;
}edge[N];
int add(int x,int y)
{
tot++;
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot;
}
void dfs(int x)
{
if(vis[x]) return ;
vis[x]=true;vist[x]=true;
for(int i=head[x];i;i=edge[i].next)
{
int to=edge[i].to;
if(!vis[to]) dfs(to);
}
vis[x]=false;
}
int main()
{
k=read(),n=read(),m=read();
;i<=k;i++) a[i]=read();
;i<=m;i++)
x=read(),y=read(),add(x,y);
;i<=k;i++)
{
memset(vist,,sizeof(vist));
dfs(a[i]);
;i<=n;i++)
if(vist[i]) sum[i]++;
}
;i<=n;i++)
if(sum[i]==k) ans++;
printf("%d",ans);
;
}
dfs
转bfs
我们枚举每一个牛的起始点,用bfs拓展一下它都能到哪里,然后让这个点的计数加1,
最后看一下都有多少点的计数是k就好了嘛。
这样看起来好像跟最短路的思路差不多,不过不用反向建边,我觉得应该更好想吧,而且拓展可达点这种问题不应该就用bfs而不是dfs吗
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 11000
using namespace std;
queue<int>q;
bool vis[N];
int k,n,m,x,y,ans,tot,a[N],sum[N],head[N];
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
struct Edge
{
int to,next,from;
}edge[N];
int add(int x,int y)
{
tot++;
edge[tot].to=y;
edge[tot].next=head[x];
head[x]=tot;
}
int main()
{
k=read(),n=read(),m=read();
;i<=k;i++) a[i]=read();
;i<=m;i++)
x=read(),y=read(),add(x,y);
;i<=k;i++)
{
memset(vis,,sizeof(vis));
q.push(a[i]);vis[a[i]]=true;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=head[x];i;i=edge[i].next)
{
int to=edge[i].to;
if(!vis[to])
{
vis[to]=true;
q.push(to);
}
}
}
;i<=n;i++)
if(vis[i]) sum[i]++;
}
;i<=n;i++)
if(sum[i]==k) ans++;
printf("%d",ans);
;
}
AC的bfs
洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic的更多相关文章
- 洛谷 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
题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...
- bzoj1648 / P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 你愿意的话,可以写dj. 然鹅,对一个缺时间的退役选手来说,暴力模拟是一个不错的选择. 让每个奶牛都把图走一遍,显然那些被每个奶牛都走 ...
- P2853 [USACO06DEC]牛的野餐Cow Picnic
------------------------- 长时间不写代码了,从学校中抽身出来真的不容易啊 ------------------------ 链接:Miku ----------------- ...
- 题解【洛谷P2853】[USACO06DEC]牛的野餐Cow Picnic
题目描述 The cows are having a picnic! Each of Farmer John's \(K (1 ≤ K ≤ 100)\) cows is grazing in one ...
- [USACO06DEC]牛的野餐Cow Picnic DFS
题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...
- 洛谷P2854 [USACO06DEC]牛的过山车Cow Roller Coaster
P2854 [USACO06DEC]牛的过山车Cow Roller Coaster 题目描述 The cows are building a roller coaster! They want you ...
- 洛谷P3080 [USACO13MAR]牛跑The Cow Run
P3080 [USACO13MAR]牛跑The Cow Run 题目描述 Farmer John has forgotten to repair a hole in the fence on his ...
- 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup
https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...
随机推荐
- jquery中arrt()和prop()的区别
在jQuery中,attr()函数和prop()函数都用于设置或获取指定的属性,它们的参数和用法也几乎完全相同. 但不得不说的是,这两个函数的用处却并不相同.下面我们来详细介绍这两个函数之间的区别. ...
- python之序列化
什么叫序列化? 序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes. 把字符转换成内存数据类型,叫反序列化. 为什么要序列化? 你 ...
- 关于stm32优先级大小的理解
转载自:https://www.cnblogs.com/ZKeJun/p/6112591.html 一. 组别:0>1>2>3>4 组别优先顺序(第0组优先级最强,第4组优 ...
- poj 2251 三维地图最短路径问题 bfs算法
题意:给你一个三维地图,然后让你走出去,找到最短路径. 思路:bfs 每个坐标的表示为 x,y,z并且每个点都需要加上时间 t struct node{ int x, y, z; int t;}; b ...
- 如何用ADMINISTRATOR登陆WIN7
Windows 7系统出于安全考虑,将系统超级管理员帐户(Administrator)隐藏了,不允许"普通用户"使用.很多时候特别是安装一些应用软件时,由于兼容的问题,普通权限的用 ...
- Python之code对象与pyc文件(一)
Python程序的执行过程 我们都知道,C语言在执行之前需要将源代码编译成可执行的二进制文件,也就是将源代码翻译成机器代码,这种二进制文件一旦生成,即可用于执行.但是,Python是否一样呢?或许很多 ...
- python week08 并发编程之多进程--实践部分
一 multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在python中大部分情况需要使用多进程.P ...
- dev c++ 提示没有iostream.h文件
dev c++ 提示没有iostream.h文件 解决办法路径没有打通最好是这样写:#include <iostream>using namespace std;int main(int ...
- X86保护模式 八操作系统类指令
X86保护模式 八操作系统类指令 通常在操作系统代码中使用,应用程序中不应用这些指令 指令分为三种:实模式指令,任何权级下使用的指令.实模式权级0下可执行的指令和仅在保护模式下执行的指令 一 实模 ...
- VMware RHEL6.3 开启网络连接
确认/etc/sysconfig/network是否存在,如果不存在,service network 命令使用不了.新建: NETWORKING=yes HOSTNAME=RHEL6. GATEWAY ...