题目描述

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 NN barns connected with MM bidirectional paths between some pairs of barns (1≤N,M≤200,000). 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.

输入

The first line of input contains N and M. The next M lines each describe a path in terms of the pair of barns it connects (barns are conveniently numbered 1…N). The final N lines give a permutation of 1…N describing the order in which the barns will be closed.

输出

The output consists of N lines, each containing "YES" or "NO". The first line indicates whether the initial farm is fully connected, and line i+1 indicates whether the farm is fully connected after the iith closing.

样例输入

4 3
1 2
2 3
3 4
3
4
1
2

样例输出

YES
NO
YES
YES


题目大意

给你n个点和m条边的无向图,有n次删点操作,删掉点后与这个点相连的边也随之删除。问删除每个点之前这个图是不是连通图。

题解

并查集

由于删点比较难搞,所以我们需要换一种思路:

可以先把所有的点删掉,然后反过来一个一个再加进来。

这样便于直接处理改动的边。

然后用一个并查集维护连通块即可。

#include <cstdio>
int head[200010] , to[400010] , next[400010] , cnt , a[200010] , f[200010] , ans[200010] , ok[200010];
int find(int x)
{
return x == f[x] ? x : f[x] = find(f[x]);
}
void add(int x , int y)
{
to[++cnt] = y;
next[cnt] = head[x];
head[x] = cnt;
}
int main()
{
int n , m , i , j , x , y , tmp = 0;
scanf("%d%d" , &n , &m);
for(i = 1 ; i <= m ; i ++ )
scanf("%d%d" , &x , &y) , add(x , y) , add(y , x);
for(i = 1 ; i <= n ; i ++ )
scanf("%d" , &a[i]);
for(i = 1 ; i <= n ; i ++ )
f[i] = i;
for(i = n ; i >= 1 ; i -- )
{
ok[a[i]] = 1;
tmp ++ ;
for(j = head[a[i]] ; j ; j = next[j])
{
if(ok[to[j]])
{
x = find(a[i]) , y = find(to[j]);
if(x != y)
{
f[x] = y;
tmp -- ;
}
}
}
ans[i] = (tmp == 1);
}
for(i = 1 ; i <= n ; i ++ )
printf("%s\n" , ans[i] ? "YES" : "NO");
return 0;
}

【bzoj4579】[Usaco2016 Open]Closing the Farm 并查集的更多相关文章

  1. BZOJ 4579: [Usaco2016 Open]Closing the Farm

    Description 依次删去一个点和它的边,问当前图是否连通. Sol 并查集. 倒着做就可以了. 每次将一个点及其的边加入,如果当前集合个数大于 1,那么就不连通. Code /******** ...

  2. hdu-1198 Farm Irrigation---并查集+模拟(附测试数据)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1198 题目大意: 有如上图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种 ...

  3. 续并查集学习笔记——Closing the farm题解

    在很多时候,并查集并不是一个完整的解题方法,而是一种思路. 通过以下题目来体会并查集逆向运用的思想. Description Farmer John and his cows are planning ...

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

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

  5. 【BZOJ 4579】【Usaco2016 Open】Closing the Farm

    http://www.lydsy.com/JudgeOnline/problem.php?id=4579 把时间倒过来,只是加点,并查集维护连通块. #include<cstdio> #i ...

  6. HDU1198水管并查集Farm Irrigation

    Benny has a spacious farm land to irrigate. The farm land is a rectangle, and is divided into a lot ...

  7. 【简单并查集】Farm Irrigation

    Farm Irrigation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tot ...

  8. HDU 1198 Farm Irrigation(并查集,自己构造连通条件或者dfs)

    Farm Irrigation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. hdu 1198 Farm Irrigation(深搜dfs || 并查集)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...

随机推荐

  1. 20145202mc《计算机病毒》实践2

    网站检测 http://www.virustotal.com太慢了实在,所以我换成了http://www.virscan.org/ lab01-01.exe 文件行为 lab01-01.dll 可以基 ...

  2. 苏州Uber优步司机奖励政策(12月28日到1月3日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  3. nodeJs 安装 npm nodeModules package.json

    Nodejs   1.安装nodejs 从nodejs官网下载最新版本的node,设置环境变量这样就可以在cmd下直接用命令行操作npm 环境变量:path  d:/nodejs 查看本机node及n ...

  4. Consul初体验

      Preface       Today I'm gonna implement a consul in my environment to discover service of MySQL da ...

  5. 180608-Git工具之Stash

    git stash 暂存 背景: 实际开发过程中,经常可能遇到的一个问题,当你在dev分支上正开发得happy的时候:突然来了个线上bug,得赶紧从release分支上切一个bugfix分支来解决线上 ...

  6. 分布式部署Apache-Jmeter粗略流程

    注意事项 Windows版和Mac版Jmeter可互相通信 确认被部署的机器安装有JDK并已配置好环境变量 Controller安装 1. 安装Jmeter,监视插件JMeterPlugins-Sta ...

  7. 试用Markdown来写东西

    试用Markdown来写东西 前言 之前有过一段时间的写东西的习惯,但是后来因为各种原因(主要是因为自己懒惰拖延),所以一直没有写,现在想再开始写,目的很明确,就是发现很多时候,写作能够很好的练习自己 ...

  8. 【Linux 运维】linux系统关机、重启、注销命令

    linux 关机.重启.注销命令: 关机命令: shutdown -h now 立刻关机(生产常用) shutdown -h  +1  一分钟后关机      (    shutdown -c 可以将 ...

  9. Docker容器的搭建

    Docker容器的搭建 一.先从Docker Hub上面拉取一个基础镜像 命令:docker pull ubuntu 命令说明:pull:拉取镜像的命令,ubuntu:拉取镜像的名称 扩展命令: 命令 ...

  10. metamask注记词

    leaf orbit poet zebra toy day put dinosaur review cool pluck throw(m) 一个钱包地址 里面有多个账号 菲苾代表了不同网络