题目描述

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. sql 语句NVL()用法

    一NVL函数是一个空值转换函数 NVL(表达式1,表达式2) 如果表达式1为空值,NVL返回值为表达式2的值,否则返回表达式1的值. 该函数的目的是把一个空值(null)转换成一个实际的值.其表达式的 ...

  2. css - 所有的a标签设置为新窗口打开

    前言 由于工作的需要,需要把某个页面下的所有a标签都设置为新开新窗口,即:<a href="XXX">增加target:<a href="XXX&quo ...

  3. SceneView 追踪选择目标

    在编辑器的Scene视图中追踪选择目标,调试动作用 SceneView这个类没有说明文档比较蛋疼 在update中调用SceneViewCameraFace2Target函数,编辑器的OnInspec ...

  4. codevs3304水果姐逛街(线段数)

    3304 水果姐逛水果街Ⅰ  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond   题目描述 Description 水果姐今天心情不错,来到了水果街. 水果 ...

  5. [Swift通天遁地]五、高级扩展-(12)扩展故事板中的元件添加本地化功能

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. 数据库部署到linux服务器,供本地访问。

    1.  将本地的sql文件上传至服务器 scp /Users/fangke/Documents/article.sql root@IP:/usr/local 2. 登陆服务器的mysql 3. 创建数 ...

  7. the interview questions of sql server

    1.一道SQL语句面试题,关于group by 表内容: 2005-05-09 胜 2005-05-09 胜 2005-05-09 负 2005-05-09 负 2005-05-10 胜 2005-0 ...

  8. python基础篇(一)-------- 字符串的操作

    1.字符串的常用操作: 已知字符串:str = "hello world zhangsan and zhangsan" 1.字符串的长度:len(str) 2.查看字符串的索引值: ...

  9. 重绘DataGridView的DataGridViewCheckBoxCell控件

    最近项目中要用到在DataGridView单元格里面放置一个带有文本的 DataGridViewCheckBoxCell控件但原有 的是不支持的然后我就想着重写个 DataGridViewCheckB ...

  10. Quartz实战

    https://my.oschina.net/yinxiaoling/blog/542336?fromerr=s3ko7u33 Quartz实战 > 一.内存型(1) <bean name ...