[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 ...
随机推荐
- 快速访问GitHub
Github网站可以访问,但是由于网络代理商的原因,造成访问速度很慢. 本文采取方法:手动更改hosts Hosts是一个没有扩展名的系统文件,可以用记事本等工具打开,其作用就是将一些常用的网址域名与 ...
- PCB genesis连孔加除毛刺孔(槽孔与槽孔)实现方法(三)
一.为什么 连孔加除毛刺孔 原因是 PCB板材中含有玻璃纤维, 毛刺产生位置在于2个孔相交位置,由于此处钻刀受力不均导致纤维切削不断形成毛刺 ,为了解决这个问题:在钻完2个连孔后,在相交处再钻一个孔, ...
- es6 import 与 export
1.export 命令 export 命令用于规定模块的对外接口. 一个模块就是一个独立的文件.该文件内部所有的变量,外部无法获取.要想外部能够读取模块内部的某个变量,就必须使用 export 关键字 ...
- Netty--数据通信和心跳检测
数据通信 概述: netty的ReadTimeOut实现方案3 服务端: public class Server { public static void main(String[] args) th ...
- 题解报告:hdu 1213 How Many Tables
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Problem Description Today is Ignatius' birthday. ...
- android webview 简单应用
一直没有用过webView 在网上找了一个小例子,主要实现以下功能: 1.当webview加载网页的时候在标题栏上显示加载进度 2.隐藏webkit浏览器的地址栏 3.设置程序的标题为网页的标题 4. ...
- Spring Cloud (6) 分布式配置中心-高可用
高可用 现在已经可以从配置中心读取配置文件了,当微服务很多时都从配置中心读取配置文件,这时可以将配置中心做成一个微服务,将其集群化,从而达到高可用. 改造config-server 加入eureka ...
- build.gradle(Mdule.app)依赖库相关
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) //noinspection GradleCompati ...
- Android项目实战_手机安全卫士程序锁
###1.两个页面切换的实现1. 可以使用Fragment,调用FragmentTransaction的hide和show方法2. 可以使用两个布局,设置visibility的VISIABLE和INV ...
- linux下tomcat启动正常,但是外部浏览器无法访问
这种情况一般是由于系统防火墙设置问题导致的,这次遇到的系统是centos 7.2,防火墙由iptables改成了firewall,因此停止防火墙的命令应该是: systemctl disable fi ...