题目描述

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.

输入输出样例

输入样例#1:

4 3
1 2
2 3
3 4
3
4
1
2
输出样例#1:

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)的更多相关文章

  1. 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm

    农夫约翰和他的奶牛准备去旅行,所以约翰想要把他的农场临时关闭. 农场有N个牛棚(牛棚从1到N编号),有M条路连接这些牛棚(1≤N,M≤3000). 约翰打算挨个关闭牛棚,在关牛棚的时候, 他突然想起一 ...

  2. 一道并查集的(坑)题:关闭农场closing the farm

    题目描述 in English: Farmer John and his cows are planning to leave town for a long vacation, and so FJ ...

  3. 洛谷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 ...

  4. 洛谷 P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver

    传送门 题目大意: n个谷仓 ,每次关闭一个谷仓,问剩下没被关闭的谷仓是 否联通. 题解:并查集+倒序处理 代码: #include<iostream> #include<cstdi ...

  5. [USACO16OPEN]关闭农场Closing the Farm_Silver

    题目描述 FJ和他的奶牛们正在计划离开小镇做一次长的旅行,同时FJ想临时地关掉他的农场以节省一些金钱. 这个农场一共有被用M条双向道路连接的N个谷仓(1<=N,M<=3000).为了关闭整 ...

  6. P3144 [USACO16OPEN]关闭农场——离线,并查集

    https://www.luogu.org/problem/P3144 每次关闭一个农场,农场之间有边相连,问每次关闭后开着的农场是否是一个连通块: 数据小,离线搞: 我们先记录删的顺序,然后倒着来, ...

  7. 缩点【洛谷P2921】 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    [洛谷P2921] [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N< ...

  8. 洛谷 P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

    题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵 ...

  9. 洛谷——P2919 [USACO08NOV]守护农场Guarding the Farm

    P2919 [USACO08NOV]守护农场Guarding the Farm 题目描述 The farm has many hills upon which Farmer John would li ...

随机推荐

  1. Kernel trick----PRML读书笔记

    Many linear parametric models can be re-cast into an equivalent 'dual representstion' in which the p ...

  2. Scala 获取当前时间

    def NowDate(): String = { val now: Date = new Date() val dateFormat: SimpleDateFormat = new SimpleDa ...

  3. java replaceAll 忽略大小写

    public static void main(String[] args) { String temp=" CLASS_path : /aaabb/"; System.out.p ...

  4. Akka源码分析-Remote-收消息

    上一遍博客中,我们分析了网络链接建立的过程,一旦建立就可以正常的收发消息了.发送消息的细节不再分析,因为对于本地的actor来说这个过程相对简单,它只是创立链接然后给指定的netty网路服务发送消息就 ...

  5. javascript中for...in和for...of的区别

    for...of循环是ES6引入的新的语法. for...in遍历拿到的x是键(下标).而for...of遍历拿到的x是值,但在对象中会提示不是一个迭代器报错.例子如下: let x; let a = ...

  6. SQL Server 2008R2 Set IDENTITY_INSERT 表名 ON/OFF不能与insert into select 的语句一起执行?

    大家都知数据库表中的列可以自增长,但是有时候我们需要插入数据的时候会指定这一列的数据. 这时候我们可以很简单的利用sql语句来执行新增一条的数据,如下: set IDENTITY_INSER 表名 o ...

  7. JQuery中常用的$.get(),$.post(),$.ajax(),$.getJSON(),load()的详解与区别

    背景:因为最近需要获取本地的数据件进行项目测试,需要用到JQuery实现数据文件的读取,但是由于对JQuery内的获取文件方式不太了解,这次趁着机会进行一下总结.因为该总结是本人根据平常的使用及网上的 ...

  8. HashMap , TreeMap , TreeMap 默认排序

    Java代码  import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java. ...

  9. 移动web——bootstrap响应式轮播图

    基本介绍 1.bootstrap有轮播图的模板,我们只需要改动下就行. 2.这里我们将介绍桌面版本和移动版本最后是综合版本 桌面版本 1.这里的图片设置是有窍门的,不再去添加img标签,而是作为a标签 ...

  10. Apache 在Linux上的安装

    1.获取源码 wget http://mirror.bit.edu.cn/apache//httpd/httpd-2.4.37.tar.gz 2.卸载centos自带的apache 3.解压apach ...