题目描述

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. 20145234黄斐《Java程序设计》第七周学习总结(课本部分)

    教材知识概述 存储器系统是一个具有不同容量.成本和访问时间的存储设备的层次结构. 6.1 存储技术 1.随机访问存储器(RAM)分为两类:静态的(SRAM)比动态的(DRAM)快,但也贵得多 静态RA ...

  2. 北京Uber优步司机奖励政策(3月23日)

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

  3. Sql Server 2008R2中使用CET进行递归查询

            在使用数据库的过程中,我们经常会遇到递归的查询.比如传入一个分类ID,要查出这个分类下的所有子分类,以及子分类的子分类.或者说传入一个部门ID,要查出这个部门下所有子部门的人员:在Or ...

  4. jmeter开发自己的sampler插件

    1. 新建maven工程 2.pom文件引入jmeter的核心包 <project xmlns="http://maven.apache.org/POM/4.0.0" xml ...

  5. SQL注入篇二------利用burp盲注,post注入,http头注入,利用burpsuit找注入点,宽字节注入

    1.布尔盲注burpsuit的使用 先自己构造好注入语句,利用burpsuit抓包,设置变量,查出想要的信息. 比如----查数据库名的ascii码得到数据库构造好语句 http://123.206. ...

  6. 一篇文章让你了解GC垃圾回收器

    简单了解GC垃圾回收器 了解GC之前我们首先要了解GC是要做什么的?顾名思义回收垃圾,什么是垃圾呢? GC回收的垃圾主要指的是回收堆内存中的垃圾对象. 从根对象出发,所有被引用的对象,都是存活对象 其 ...

  7. JavaScript --经典问题

    JavaScript中如何检测一个变量是一个String类型?请写出函数实现 方法1. function isString(obj){ return typeof(obj) === "str ...

  8. PAT-甲级解题目录

    PAT甲级题目:点这里 pat解题列表 题号 标题 题目类型  10001 1001 A+B Format (20 分)  字符串处理  1003 1003 Emergency (25 分) 最短路径 ...

  9. day-18 滑动平均模型测试样例

    为了使训练模型在测试数据上有更好的效果,可以引入一种新的方法:滑动平均模型.通过维护一个影子变量,来代替最终训练参数,进行训练模型的验证. 在tensorflow中提供了ExponentialMovi ...

  10. opencv-学习笔记(4)-模糊

    opencv-学习笔记(4)-模糊 本章要点: 4种模糊方式 2d卷积 Cv2.filter2D(‘图像对象’,‘目标图像这里直接设为-1即可’,kernal,anchor(-1,-1)) 一般后一个 ...