洛谷——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 ...
随机推荐
- npm 安装express npm ERR! code UNABLE_TO_VERIFY_LEAF_SIGNATURE
npm安装总是报错 报错信息: 解决方案: 在命令行输入: npm config set strict-ssl false 设置之后即可安装软件:
- debian安装中文字体
debian刚安装完成之后,因为没有中文字体,会出现方框. 安装中文字体: $ su # apt-get install fonts-arphic-bkai00mp fonts-arphic-bsmi ...
- centos7 bond 和 网桥配置
rhel7系统bond配置(更新版本):https://www.cnblogs.com/zhangjianghua/p/9119808.html Bonding的模式一共有7种: 1.mode=0(b ...
- while循环输出的表格
方法一: <?php echo '<table border="1" width="800" align="center"> ...
- Aizu 2450 Do use segment tree 树链剖分
题意: 给出一棵\(n(1 \leq n \leq 200000)\)个节点的树,每个节点有一个权值. 然后有\(2\)种操作: \(1 \, a \, b \, c\):将路径\(a \to b\) ...
- eureka显示ip地址的参数
eureka.instance.prefer-ip-address=trueeureka.instance.instance-id=${#spring.cloud.client.ipAddress}: ...
- Selenium WebDriver- 操作 IFrame 中的页面元素
#encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...
- Leetcode12--->Integer to Roman(整数转换为罗马数字)
题目: 给定一个整数,将其转换为罗马数字; 题目很简单,主要是依靠整数和罗马数字的对应表: I= 1:V= 5: X = 10: L = 50: C = 100: D = 500: M = 1000 ...
- day03_08 变量的重新赋值02
python自动回收垃圾内存,不用了自动会回收,但是C不会 以下del代码为手动强拆,就是从内存中删除变量名
- jqery实现一个图标上下滑动效果
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...