题目描述

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. 北京Uber优步司机奖励政策(3月1日)

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

  2. CF 741 D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

    D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths http://codeforces.com/problemset/probl ...

  3. mysql主从集群搭建;(集群复制数据)

    1.搭建mysql 5.7环境chown mysql:mysql -R /data/groupadd mysqluseradd -g mysql mysql yum install numactlrp ...

  4. Error starting mongod. /var/run/mongodb/mongod.pid exists.启动mongodb报错

    linux上安装mongodb,启动时报上面的错,解决如下: 解决方法: 1.删除mongod.pid文件 rm -rf /var/run/mongodb/mongod.pid 2.修改/tmp/mo ...

  5. Linux搭建mysql、apache、php服务总结

    本随笔文章,由个人博客(鸟不拉屎)转移至博客园 写于:2018 年 04 月 22 日 原地址:https://niaobulashi.com/archives/linux-mysql-apache- ...

  6. C# 中访问修饰符

    1.public 完全公开的,公共的 2. private 私有的,只能在当前类的内部访问, 不可修饰类 3.protected 受保护的,只能在当前类的内部以及其子类中访问,不能用来修饰类 4.in ...

  7. 【if控制器】-(某种情况成立就执行的场景)

    if 控制器   一般来判断某种特殊情况 成立,就执行. JEXL Expression to evaluate:此处直接填写需要进行判断的表达式即可 表达式支持: ==  是否等于,如${__jex ...

  8. vim—自动缩进(编写Python脚本)

    大神推荐使用vim编写Python脚本,学而时积之,不亦乐乎! 使用vim编写Python脚本的时候不能正常缩进,需要修改vimrc文件 Ubuntu系统下vimrc文件的位置: $ cd /etc/ ...

  9. linux服务器操作小技巧

    python程序后台一直运行,并将打印信息输出到文件中 nohup -u test.py > out.txt & -u 表示无缓冲,直接将打印信息输出带文件中 &表示程序后台运行

  10. Struts2文件上传带进度条,虽然不是很完美

    好久没有写东西,最近在做个项目,要用到文件h 传的,以前虽然也做上传,但是总觉得不好用 ,现在和队友合作做了一个带进度条的上传,觉得还行~~和大家分享一下. 首先说一下大概是这样实现的,在我们平时的上 ...