在很多时候,并查集并不是一个完整的解题方法,而是一种思路。

通过以下题目来体会并查集逆向运用的思想。

Description

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.

Input

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 o

f 1…N describing the order in which the barns will be closed.

Output

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.

Sample Input

4 3

1 2

2 3

3 4

3

4

1

2

Sample Output

YES

NO

YES

YES

显然,按照正向逻辑,每次删去一条边都必须检查整幅图的连通性,做法过于冗杂,时间复杂度高。换一种思维,我们将一个一个点加进图中,通过并查集来维护图的连通性,则可以再Om的时间之内完成。程序如下。

#include<iostream>
#include<cstdio>
using namespace std;
struct line{
int to;
int next;
}; line a[];
int head[];
int n,m,be[],ans[],fa[],que[];
int getf(int k){ //并查集常规+路径压缩
if(fa[k]!=k)fa[k]=getf(fa[k]);
return fa[k];
}
int main(){
cin>>n>>m;
for(int i=;i<=m;++i){
int x,y;
scanf("%d%d",&x,&y);
a[*i-].next=head[x]; //每条边看做两条单向边处理,运用链式前向星保证空间充足
a[*i-].to=y;
head[x]=*i-;
a[*i].next=head[y];
a[*i].to=x;
head[y]=*i;
}
for(int i=n;i>=;--i)scanf("%d",&que[i]); for(int i=;i<=n;++i)fa[i]=i; int num=;
be[que[]]=;
ans[]=; for(int i=;i<=n;++i){
num++; //加入一个新的点,num记录当前图中的集合个数,只有一个集合时说明图连通
be[que[i]]=; //bei表示这个点已经加入图中
int now=head[que[i]];
while(now!=){
if(be[a[now].to]==){
int fx=getf(a[now].to);
if(fx!=que[i]){
num--;
fa[fx]=que[i];
}
}
now=a[now].next;
}
if(num==)ans[i]=;
else ans[i]=;
} for(int i=n;i>=;--i){ //逆向输出
if(ans[i]==)printf("YES\n");
else printf("NO\n");
}
return ;
}

To be continue......

续并查集学习笔记——Closing the farm题解的更多相关文章

  1. 续并查集学习笔记——Gang团伙题解

    一言不合先贴题目 Description 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌人的敌人是我的朋友: 所有是朋友的人组成一个团伙 ...

  2. 边带权并查集 学习笔记 & 洛谷P1196 [NOI2002] 银河英雄传说 题解

    花了2h总算把边带权并查集整明白了qaq 1.边带权并查集的用途 众所周知,并查集擅长维护与可传递关系有关的信息.然而我们有时会发现并查集所维护的信息不够用,这时"边带权并查集"就 ...

  3. 【日常学习】【并查集+map】codevs2639 约会计划题解

    然而我居然让诸城一中悲剧机房的C++可以编译了··· 直接上题目 题目描写叙述 Description cc是个超级帅哥,口才又好.rp极高(这句话似乎降rp),又非常的幽默,所以非常多mm都跟他关系 ...

  4. [Bzoj4195] [NOI2015] 程序自动分析 [并查集,哈希,map] 题解

    用并查集+离散化,注意:并查集数组大小不是n而是n*2 #include <iostream> #include <algorithm> #include <cstdio ...

  5. CTFHub Web题学习笔记(SQL注入题解writeup)

    Web题下的SQL注入 1,整数型注入 使用burpsuite,?id=1%20and%201=1 id=1的数据依旧出现,证明存在整数型注入 常规做法,查看字段数,回显位置 ?id=1%20orde ...

  6. CDQ分治学习笔记(三维偏序题解)

    首先肯定是要膜拜CDQ大佬的. 题目背景 这是一道模板题 可以使用bitset,CDQ分治,K-DTree等方式解决. 题目描述 有 nn 个元素,第 ii 个元素有 a_iai​.b_ibi​.c_ ...

  7. 并查集 (Union-Find Sets)及其应用

    定义 并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.常常在使用中以森林来表示. 集就是让每个元素构成一个单元素的集合,也就是按一定顺序将属于同一组的 ...

  8. Redis学习笔记一:数据结构与对象

    1. String(SDS) Redis使用自定义的一种字符串结构SDS来作为字符串的表示. 127.0.0.1:6379> set name liushijie OK 在如上操作中,name( ...

  9. 九度OJ 1446 Head of a Gang -- 并查集

    题目地址:http://ac.jobdu.com/problem.php?pid=1446 题目描述: One way that the police finds the head of a gang ...

随机推荐

  1. iOS控制器的生命周期分析和使用

    转自http://blog.csdn.net/qijianli/article/details/7826979 iOS的SDK中提供很多原生ViewController,大大提高了我们的开发效率,下面 ...

  2. 如何设置jvm内存

    本文向大家简单介绍一下进行JVM内存设置几种方法,安装Java开发软件时,默认安装包含两个文件夹,一个JDK(Java开发工具箱),一个JRE(Java运行环境,内含JVM),其中JDK内另含一个JR ...

  3. jq弹框确认

    function delCustomer(id,num){ var r=confirm("友情提醒:确认要删除客户吗?"); if (r==true){ $.ajax({ type ...

  4. 终端检测HTTPS服务端

    nscurl --ats-diagnostics --verbose https:ssl.tianwensm.xyz

  5. php7.0.12 laravel 链接sqlserver数据库

    https://www.microsoft.com/en-us/download/details.aspx?id=20098 下载最后一个,然后这个工具可以将dll扩展下载下来,选择一个空白的文件夹就 ...

  6. Abundant Resources

    https://github.com/vhf/free-programming-books/blob/master/free-programming-books-zh.md

  7. Linux opencv安装与编译

    参考http://blog.csdn.net/solomon1558/article/details/51967280 1安装cmake以及依赖库 $ sudo apt-get install cma ...

  8. hashmap的底层实现

    HashMap的底层实现都是数组+链表结构实现的,添加.删除.获取元素都是先计算hash值,根据hash值和table.length计算出index也就是table的数组的下标,然后进行相应的操作. ...

  9. 1-MySQL命令行

    0.显示数据库 show databases; 1.选择数据库 use 数据库名; 2.显示数据库中的表 show tables; 3.显示数据表的结构 describe 表名; 4.显示表中记录 S ...

  10. 。。。学习CSS3的第一个属性border-radius。。。

    学习border-radius,感觉这个文档写的很不错: http://blog.sina.com.cn/s/blog_61671b520101gelr.html