农夫约翰和他的奶牛准备去旅行,所以约翰想要把他的农场临时关闭。

农场有N个牛棚(牛棚从1到N编号),有M条路连接这些牛棚(1≤N,M≤3000)。

约翰打算挨个关闭牛棚,在关牛棚的时候,

他突然想起一个有趣的问题:剩余的这些没有关闭的牛棚是不是连通呢?

连通指的是从任何一个牛棚出发,都能到达其他牛棚(注意:已经关闭的牛棚不可以通行)。

Input

第一行包括两个整数N M,

接下来M行,每行输入两个整数x y,表示x和y牛棚之间存在一条路,路是双向通行的。

接下来n行,表示关牛棚的顺序。

Output 输出N行:

第一行表示初始状态下,牛棚是否连通;

接下来N-1行,表示关闭对应牛棚后,剩余牛棚是否联通。

如果连通,输出YES,不连通,输出NO。(最后一次关闭不需要输出)。

————————————————————————————————————————

回来补一波并查集

这里我们做一波离线处理 将删点转换为加点 也就是将关闭变为开启

那么我们每次开启一个牛棚x  k++(k指的是当前联通快的个数)

然后我们枚举x的所有出路 找到已经开始的牛棚 如果他们不在一个了联通块内k-- 合并两个块

最后如果这个时刻k是1 那么就是联通 否则不是

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int M=;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int n,m,k,f[M],h[M],usd[M];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
int first[M],cnt,c[M];
struct node{int to,next;}e[*M];
void ins(int a,int b){
cnt++; e[cnt].to=b; e[cnt].next=first[a]; first[a]=cnt;
}
void insert(int a,int b){ins(a,b); ins(b,a);}
int main()
{
int x,y;
n=read(); m=read();
for(int i=;i<=n;i++) f[i]=i;
for(int i=;i<=m;i++) x=read(),y=read(),insert(x,y);
for(int i=;i<=n;i++) c[i]=read();
for(int i=n;i>=;i--){
k++; usd[c[i]]=;
int now=c[i],p=find(now);
for(int j=first[now];j;j=e[j].next){
if(!usd[e[j].to]) continue;
int q=find(e[j].to);
if(p==q) continue;
f[q]=p; k--;
}
h[i]=k;
}
for(int i=;i<=n;i++)
if(h[i]==) printf("YES\n");
else printf("NO\n");
return ;
}

洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm的更多相关文章

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

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

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

  3. [USACO16OPEN]关闭农场Closing the Farm(洛谷 3144)

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

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

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

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

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

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

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

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

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

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

    题目描述 The farm has many hills upon which Farmer John would like to place guards to ensure the safety ...

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

    https://www.luogu.org/problem/show?pid=2919 题目描述 The farm has many hills upon which Farmer John woul ...

随机推荐

  1. python__系统 : socket_TCP相关

    tcp和udp对比起来.还是tcp相对稳定一些,但是因为有三次挥手和四次握手,以及确认包(ack)的存在,可能在速度上会比udp慢. 用python的socket模块可以建立tcp服务端: from ...

  2. POJ 2079 最大三角形面积(凸包)

    Triangle Description Given n distinct points on a plane, your task is to find the triangle that have ...

  3. PHP.18-图片等比例缩放

    图片等比例缩放 自定义函数ImageUpdateSize($pricname, $maxx, $maxy, $pre) 1.$pricname:被缩放的图片源(路径):2.$maxx,$maxy:缩放 ...

  4. PHP.15-mysqli

    从PHP5.0开始可以使用mysql(i), 是一个面向对象的技术(新加功能都会以对象形式添加) i:表示改进,1. 功能增加了, 2,效率大大增加(以后的PHP项目改成mysqli),3,更稳定 m ...

  5. 为什么不要使用 Async Void ?

    原文:为什么不要使用 Async Void ? 问题 在使用 Abp 框架的后台作业时,当后台作业抛出异常,会导致整个程序崩溃.在 Abp 框架的底层执行后台作业的时候,有 try/catch 语句块 ...

  6. 从库函数操作RCC的流程来理解偏移变量

    下面是库函数操作RCC流程,看完后有我的疑问:偏移地址的理解 1,库函数直接操作:RCC库函数操作  RCC_APB2PeriphClockCmd ()RCC->APB2ENR |= RCC_A ...

  7. P3817 小A的糖果(洛谷月赛)

    P3817 小A的糖果 题目描述 小A有N个糖果盒,第i个盒中有a[i]颗糖果. 小A每次可以从其中一盒糖果中吃掉一颗,他想知道,要让任意两个相邻的盒子中加起来都只有x颗或以下的糖果,至少得吃掉几颗糖 ...

  8. android:windowBackground 和 Android:background 的区别

    通过问别人,我知道了android:windowBackground 和 Android:background的区别 android:windowBackground 一般用于activity启动的时 ...

  9. Android webview 加载https网页显示空白

    http://www.2cto.com/kf/201110/108836.html 这个网址讲的不错. 设置webview支持https的方法: webView.setWebViewClient(ne ...

  10. FMDB的线程安全

    最近面试被问到FMDB的多线程处理问题,因为之前项目中是移植别人的代码,没有踩过这里的坑. 问题: 多线程同时访问数据库时,报数据库锁定的问题,错误信息是: Unknown error finaliz ...