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

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

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. java中一些定时器的使用

    一:简单说明 ScheduleExecutorService接口中有四个重要的方法,其中scheduleAtFixedRate和scheduleWithFixedDelay在实现定时程序时比较方便. ...

  2. C# 实现函数回调

    public class Lib { public delegate void UserFunctionCB(); private static UserFunctionCB m_userFnCB; ...

  3. (转载)solr时区问题解决方案

    solr默认的使用的是utc格林尼治时间,与我们的GMT+8相差8个小时,网上好多解决办法是在自己应用中的时间上加8个小时和减8个小时做变换:或者不用date类型,改为long. 个人感觉这两个办法都 ...

  4. 使用flume-ng聚合双活Nginx日志

    前不久使用Keepalived搭建了Nginx双活代理服务器,以达到一个公网IP后支持多个云主机的多个域名网站的目的.完成后又想在这双活的Nginx上有所有访问网站的日志,之前有了解过Google A ...

  5. CentOS7下Apache及Tomcat开启SSL

    安装: 复制代码 yum install -y openssl #使用openssl可手动创建证书 yum install -y httpd yum install -y mod_ssl 防火墙打开8 ...

  6. swift基础:第四部分:对函数和闭包的深入

    ()之前在什么公司,都自己做过哪些项目,从架构的角度来谈谈你的项目. () 你对iOS不同版本是怎么看的,你在做项目的过程当中,是如何应对版本问题的. () 你对iOS的性能是怎么优化的. () 你通 ...

  7. 我的第二个FluentNHibernate例子with Knockout

    在上一篇我的第一个FluentNHibernate例子的基础上,我们用上knockoutjs 1用nuget添加knockoutjs包 2用nuget添加json.net包 3..在Index.csh ...

  8. 推荐!手把手教你使用Git

    推荐!手把手教你使用Git 原文出处: 涂根华的博客   http://blog.jobbole.com/78960/ 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与 ...

  9. Swift

    1.Swift项目与OC项目的区别 - Swift的类文件是以 .swift 结尾 - main.m 不见了,程序入口以 `@UIApplicationMain` - Swift中,代码都是写到 `{ ...

  10. [T-SQL]INSERT INTO SELECT 与 SELECT INTO FROM

    1.INSERT INTO SELECT 语法:INSERT INTO Table2(field1,field2,...) select value1,value2,... from Table1 要 ...