[USACO16OPEN]关闭农场Closing the Farm(洛谷 3144)
题目描述
Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to temporarily close down his farm to save money in the meantime.
The farm consists of barns connected with
bidirectional paths between some pairs of barns (
). To shut the farm down, FJ plans to close one barn at a time. When a barn closes, all paths adjacent to that barn also close, and can no longer be used.
FJ is interested in knowing at each point in time (initially, and after each closing) whether his farm is "fully connected" -- meaning that it is possible to travel from any open barn to any other open barn along an appropriate series of paths. Since FJ's farm is initially in somewhat in a state of disrepair, it may not even start out fully connected.
FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱。
这个农场一共有被用M条双向道路连接的N个谷仓(1<=N,M<=3000)。为了关闭整个农场,FJ 计划每一次关闭掉一个谷仓。当一个谷仓被关闭了,所有的连接到这个谷仓的道路都会被关闭,而且再也不能够被使用。
FJ现在正感兴趣于知道在每一个时间(这里的“时间”指在每一次关闭谷仓之后的时间)时他的农场是否是“全连通的”——也就是说从任意的一个开着的谷仓开始,能够到达另外的一个谷仓。注意自从某一个时间之后,可能整个农场都开始不会是“全连通的”。
输入输出格式
输入格式:
The first line of input contains and
. The next
lines each describe a
path in terms of the pair of barns it connects (barns are conveniently numbered
). The final
lines give a permutation of
describing the order in which the barns will be closed.
输出格式:
The output consists of lines, each containing "YES" or "NO". The first line
indicates whether the initial farm is fully connected, and line indicates
whether the farm is fully connected after the th closing.
输入输出样例
4 3
1 2
2 3
3 4
3
4
1
2
YES
NO
YES
YES
/*
每删一个点,用宽搜灌水法求是否联通。
*/
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#define M 3010
using namespace std;
int head[M],clo[M],vis[M],n,m;
struct node
{
int v,pre;
};node e[M*];
void add(int i,int x,int y)
{
e[i].v=y;
e[i].pre=head[x];
head[x]=i;
}
bool bfs(int s)
{
memset(vis,,sizeof(vis));
queue<int> q;
q.push(s);vis[s]=;
while(!q.empty())
{
int u=q.front();q.pop();
for(int i=head[u];i!=-;i=e[i].pre)
if(!vis[e[i].v]&&!clo[e[i].v])
{
vis[e[i].v]=;
q.push(e[i].v);
}
}
for(int i=;i<=n;i++)
if(!clo[i]&&!vis[i])return false;
return true;
}
int main()
{
memset(head,-,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(i*-,x,y);add(i*-,y,x);
}
for(int t=;t<=n;t++)
{
for(int i=;i<=n;i++)
if(!clo[i])
{
if(bfs(i))printf("YES\n");
else printf("NO\n");
break;
}
int x;scanf("%d",&x);
clo[x]=;
}
return ;
}
[USACO16OPEN]关闭农场Closing the Farm(洛谷 3144)的更多相关文章
- 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm
农夫约翰和他的奶牛准备去旅行,所以约翰想要把他的农场临时关闭. 农场有N个牛棚(牛棚从1到N编号),有M条路连接这些牛棚(1≤N,M≤3000). 约翰打算挨个关闭牛棚,在关牛棚的时候, 他突然想起一 ...
- 一道并查集的(坑)题:关闭农场closing the farm
题目描述 in English: Farmer John and his cows are planning to leave town for a long vacation, and so FJ ...
- 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to tem ...
- 洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
传送门 题目大意: n个谷仓 ,每次关闭一个谷仓,问剩下没被关闭的谷仓是 否联通. 题解:并查集+倒序处理 代码: #include<iostream> #include<cstdi ...
- [USACO16OPEN]关闭农场Closing the Farm_Silver
题目描述 FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱. 这个农场一共有被用M条双向道路连接的N个谷仓(1<=N,M<=3000).为了关闭整 ...
- P3144 [USACO16OPEN]关闭农场——离线,并查集
https://www.luogu.org/problem/P3144 每次关闭一个农场,农场之间有边相连,问每次关闭后开着的农场是否是一个连通块: 数据小,离线搞: 我们先记录删的顺序,然后倒着来, ...
- 缩点【洛谷P2921】 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
[洛谷P2921] [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...
- 洛谷 P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵 ...
- 洛谷——P2919 [USACO08NOV]守护农场Guarding the Farm
P2919 [USACO08NOV]守护农场Guarding the Farm 题目描述 The farm has many hills upon which Farmer John would li ...
随机推荐
- 杂项-JSP-Runoob:JSP 标准标签库(JSTL)
ylbtech-杂项-JSP-Runoob:JSP 标准标签库(JSTL) 1.返回顶部 1. JSP 标准标签库(JSTL) JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通 ...
- 48.EXt.Data.JsonReader()
转自:https://blog.csdn.net/huoyanxueren/article/details/2662915?utm_source=blogxgwz6 extJs 2.1学习笔记(Ext ...
- 客户端JavaScript Ajax
创建: 2017/10/21 完成: 2017/10/23 [TODO] 对Ajax收发各类型数据制作模板 补充跨域通信(cross origin) p457 HTTP通信 HTTP 超文本 ...
- E20171016-mk
chaos n. 混乱,紊乱; (天地未出现的) 浑沌世界; 〈古〉无底深渊; 一团糟;
- [App Store Connect帮助]一、 App Store Connect 使用入门(4)iOS 版 App Store Connect
通过 iOS 版 App Store Connect,您可以在移动设备上查看销售数据.App 元数据和顾客评论.您还可以检查 App 状态.发布您 App 的新版本并回应“Resolution Cen ...
- Vue使用html2canvas将页面转化为图片
需求是微信端将页面截屏之后保存到本地,使用了html2canvas插件 先引入插件 npm install --save html2canvas 之后在你所需要使用的页面引入 import html2 ...
- ASP.NET MVC5 之 Log4Net 的学习和使用
最近在学习 log4Net 插件,在博客园找到了好多资料,但是实现起来还是有点麻烦. 现在记录下学习的过程,期间可能加载着借鉴和转载的代码. 1.配置文件的设置: 新建config文件夹下 log4n ...
- 327 Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- Unity Toast插件(UGUI版)
简介 介于自己之前经历的一些开发过程,非常希望unity能有类似Toast的功能用于一些简单的信息显示.但是找来找去找了半天,都木有发现,实在是憋不住了,自己写了个,感觉还可以用,发出来共享一下... ...
- 跨服务器进行SQL Server数据库的数据处理
exec sp_addlinkedserver 'ITDB', ' ', 'SQLOLEDB', '服务器IP' exec sp_addlinkedsrvlogin 'ITDB', 'false ', ...