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

news/2024/11/20 13:29:31/

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

Time Limit: 5 Sec   Memory Limit: 64 MB
Submit: 514   Solved: 316
[ 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行输入KNM.接下来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



题解:从每个奶牛dfs,记录是否可以到达。枚举n个牧场。

五个字:暴力大法好。

<span style="font-size:18px;">#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll long long
using namespace std;
int read()
{int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
struct Node{int to,next;
}e[10005];
int head[1005],tot=0,K,N,M,ans,a[105];
bool v[105][1005];
void add(int u,int v)
{e[++tot]=(Node){v,head[u]};head[u]=tot;
}
void dfs(int num,int x)
{v[num][x]=1;for(int i=head[x];i;i=e[i].next){if(!v[num][e[i].to])dfs(num,e[i].to);}
}
int main()
{K=read();N=read();M=read();for(int i=1;i<=K;i++)a[i]=read();int x,y;for(int i=1;i<=M;i++){x=read();y=read();add(x,y);}for(int i=1;i<=K;i++)dfs(i,a[i]);for(int i=1;i<=N;i++){bool ok=1;for(int j=1;j<=K;j++)if(!v[j][i]){ok=false;break;}if(ok==1)ans++;}cout<<ans<<endl;
}</span>


http://www.ppmy.cn/news/286537.html

相关文章

产品要新意更要“全套解决方案”!山姆让露营玩出“风格”

文丨熔财经 作者|一城 横扫郊野、趋之若鹜&#xff0c;露营的突然火爆催生了露营经济学。从场地服务&#xff0c;到露营用具&#xff0c;再到吃喝玩乐&#xff0c;大量的市场机遇集中涌现&#xff0c;而户外品牌、电商平台等等&#xff0c;想要沾一沾露营热度的企业数不胜数。…

北京野外烧烤

呵呵,上庄我去过好几次啦,很不错啊. 如果是班里出去玩建议你先统计人数,十人以上就租车去吧,在颐和园北十五公里,不过我们都是坐车去的,坐车也很方便,在农大或者颐和山庄换乘652或者303路车到上庄水库站即可到达,下了车往回走一点就是水库了,过桥后的那个路口往前,走八百米有一…

野餐计划

题目 大佬讲解 这个是算法进阶指南上面的题目&#xff0c;ACwing上面的秦大佬讲的很详细 #include <bits/stdc.h> using namespace std; const int N1010; #define quick() ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)//读入优化 #define mem(a,b) memset(a,b,…

蔬菜视觉分拣机器人的设计与实现(RoboWork参赛方案)

蔬菜视觉分拣机器人的设计与实现 本文参加新星计划人工智能(Pytorch)赛道&#xff1a;https://bbs.csdn.net/topics/613989052 文章目录 蔬菜视觉分拣机器人的设计与实现1. 技术栈背景2. 整体设计3. 机械结构3.1 整体结构3.2 底座结构3.3 小臂结构3.4 大臂结构3.5 负载组件结…

野餐规划

一群小丑演员&#xff0c;以其出色的柔术表演&#xff0c;可以无限量的钻进同一辆汽车中&#xff0c;而闻名世界。现在他们想要去公园玩耍&#xff0c;但是他们的经费非常紧缺。他们将乘车前往公园&#xff0c;为了减少花费&#xff0c;他们决定选择一种合理的乘车方式&#xf…

露营野餐

周六跟儿子约好的公园露营野餐&#xff0c;带上行装&#xff0c;简单的食物我们出发了。兴隆公园&#xff0c;朝阳公园&#xff0c;奥体森林公园三选一&#xff0c;兴隆公园太小&#xff0c;奥体太远&#xff0c;所以敲定坐标朝阳公园。 今天天气很好&#xff0c;蓝天白云&…

出去野餐怎么发朋友圈 烧烤配啤酒怎么接下一句

在平时的生活学习中&#xff0c;我们会不断筛选和摘抄自己认为的好词好句&#xff0c;各种好词好句都是创作的灵感&#xff0c;你喜欢什么类型的句子呢&#xff1f;为满足您的需求&#xff0c;小编特地编辑了“朋友圈怎么发野餐的话 烧烤配啤酒怎么接下一句(优质)”&#xff0c…

海外户外大品牌也用WotoHub做露营用具营销?

2022清明假期期间&#xff0c;携程【露营】搜索量环比上涨98%&#xff1b;而天猫数据显示&#xff0c;3月后&#xff0c;和露营相关的产品基本以2倍以上的增速步入旺季。中国老百姓越来越喜爱“露营”了&#xff0c;甚至越来越多的消费者愿意为“精致露营”持续买单。 卧兔网络…