续并查集学习笔记——Closing the farm题解
在很多时候,并查集并不是一个完整的解题方法,而是一种思路。
通过以下题目来体会并查集逆向运用的思想。
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题解的更多相关文章
- 续并查集学习笔记——Gang团伙题解
一言不合先贴题目 Description 在某城市里住着n个人,任何两个认识的人不是朋友就是敌人,而且满足: 1. 我朋友的朋友是我的朋友: 2. 我敌人的敌人是我的朋友: 所有是朋友的人组成一个团伙 ...
- 边带权并查集 学习笔记 & 洛谷P1196 [NOI2002] 银河英雄传说 题解
花了2h总算把边带权并查集整明白了qaq 1.边带权并查集的用途 众所周知,并查集擅长维护与可传递关系有关的信息.然而我们有时会发现并查集所维护的信息不够用,这时"边带权并查集"就 ...
- 【日常学习】【并查集+map】codevs2639 约会计划题解
然而我居然让诸城一中悲剧机房的C++可以编译了··· 直接上题目 题目描写叙述 Description cc是个超级帅哥,口才又好.rp极高(这句话似乎降rp),又非常的幽默,所以非常多mm都跟他关系 ...
- [Bzoj4195] [NOI2015] 程序自动分析 [并查集,哈希,map] 题解
用并查集+离散化,注意:并查集数组大小不是n而是n*2 #include <iostream> #include <algorithm> #include <cstdio ...
- CTFHub Web题学习笔记(SQL注入题解writeup)
Web题下的SQL注入 1,整数型注入 使用burpsuite,?id=1%20and%201=1 id=1的数据依旧出现,证明存在整数型注入 常规做法,查看字段数,回显位置 ?id=1%20orde ...
- CDQ分治学习笔记(三维偏序题解)
首先肯定是要膜拜CDQ大佬的. 题目背景 这是一道模板题 可以使用bitset,CDQ分治,K-DTree等方式解决. 题目描述 有 nn 个元素,第 ii 个元素有 a_iai.b_ibi.c_ ...
- 并查集 (Union-Find Sets)及其应用
定义 并查集是一种树型的数据结构,用于处理一些不相交集合(Disjoint Sets)的合并及查询问题.常常在使用中以森林来表示. 集就是让每个元素构成一个单元素的集合,也就是按一定顺序将属于同一组的 ...
- Redis学习笔记一:数据结构与对象
1. String(SDS) Redis使用自定义的一种字符串结构SDS来作为字符串的表示. 127.0.0.1:6379> set name liushijie OK 在如上操作中,name( ...
- 九度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 ...
随机推荐
- Quartz2D 编程指南(一)概览、图形上下文、路径、颜色与颜色空间
概览 图形上下文 路径 颜色与颜色空间 变换 图案 阴影 渐变 透明层 Quartz 2D 中的数据管理 位图与图像遮罩 CoreGraphics 绘制 Layer 0.说明 本篇博客主要是对官方文档 ...
- git学习--常用命令
1.初始化一个 Git 仓库 git init <directory> //参数 directory:作为Git仓库的目录 2.从Git 仓库中拷贝项目 git clone <rep ...
- delphi之事件
delphi的事件如上图所示: 图中oncloseup代表的是日期选择下拉框关闭时触发的事件. //事件定义 procedure Ondatechange(Sender: TObject); //事件 ...
- [Bug] 解决透明 Activity 在 Android 6.0 背景不透明
如何复现 连续启动两个 Activity ,其中 Activity 1 为 不透明 的 Activity Activity 2 为 透明 的 Activity 通常用于引导页面,例如:豌豆夹锁屏引导用 ...
- 寿司点餐系统Sprint1总结
为期十天的一个冲刺,说长不长,说短不短.从一开始的接收课程任务到第一次聚集讨论. 确定方案.实行方案,再到最后的决定结束第一个冲刺,都是大家一起讨论着加小小的默契一步步 向前.没有完美,但是总体完成的 ...
- PostgreSQL function examples
warehouse_db=# CREATE TABLE warehouse_tbl(warehouse_id INTEGER NOT NULL,warehouse_name TEXT NOT NULL ...
- PhpStorm 4.0 & 5.0 部署本地Web应用 (转)
1.创建新的项目(project),创建完成之后单击工具栏的应用运行/调试(Select Run/Debug Configuration)的下拉菜单弹出 Edit Cofigurations选项,单击 ...
- 批处理——服务器的web文件备份
首先建立三个文本文件,稍后会变成.bat结尾的批处理文件. 第一个文件:copyfile.bat[复制需要备份的文件到tmp文件下,等待压缩时使用] xcopy "D:\Webhost\*. ...
- easyUI 复选框批量操作
前台js代码 function destroyExcelout(){ //返回选中多行 var row = $('#dg').datagrid('getSelections' ...
- SSH整合报错:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped[......]
非常诡异的报错,信息如下:org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select count(* ...