题目描述

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. day 3 创建窗口,移动-函数版

    1.创建窗口 #-*- coding:utf-8 -*- import pygame import time def main(): #1.创建窗口 screen = pygame.display.s ...

  2. 笔记:ndk-stack和addr2line

    笔记:关于ndk开发调试时,获取崩溃堆栈方法 1. 使用ndk-stack 直接获取c/c++崩溃代码的文件名和行号 adb shell logcat | ndk-stack -sym $PROJEC ...

  3. 92套AE抖音快闪模板(精品)

    包含很多场景和类型,直接用即可,下载地址:百度网盘,https://pan.baidu.com/s/1bRFql1zFWyfpTAwa6MhuPA 内容截图:    

  4. 提权基础-----mysql-udf提权

    1.总结关于udf提权方法 通过弱口令,爆破,网站配置文件等方式得到mysql数据库帐号密码,---还要能外连 (1).将udf.dll代码的16进制数声明给my_udf_a变量 set @my_ud ...

  5. HTML+JS = 网站注册界面源代码

    本注册页面未设置编码方式和兼容性,已测试,在Chrome浏览器显示正常 <!DOCTYPE html> <html> <head> <title>注册页 ...

  6. 【聚合报告】- 秒懂jmeter

  7. django 增删改查操作 数据库Mysql

    下面介绍一下django增删改查操作: 1.view.py # -*- coding: utf-8 -*-from __future__ import unicode_literalsfrom dja ...

  8. 【转】网游服务器中的GUID(唯一标识码)实现-基于snowflake算法

    本文中的算法采用twitter的snowflake算法,具体请搜索介绍,原来是用Scala写的,因我项目需要,改写成C++语言,主要用于高效的生成唯一的ID, 核心算法就是毫秒级时间(41位)+机器I ...

  9. 腾讯地图和百度地图的PHP相互转换

    /** * 百度地图---->腾讯地图 * @param double $lat 纬度 * @param double $lng 经度 * @return array(); */ functio ...

  10. Python3 条件与循环

    1.条件控制 下面是一个简单的条件控制语句 s=input('Please input a str: ') if s=='python': print('I love python!') elif s ...