续并查集学习笔记——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 ...
随机推荐
- WP8.1 状态栏隐藏
StatusBar statusbar = StatusBar.GetForCurrentView(); await statusbar.HideAsync();
- DEDE有无缩略图如何调取
同一样式分开调取 [field:array runphp='yes']@me = (strpos(@me['litpic'],'defaultpic') ? "":"&l ...
- 真机远程调试 ( IOS Android 以及微信,weex)
1.以前cordova远程调试,Android的直接连接USB后,用chrome打开chrome://inspect网址 IOS的打开Safari的developer下. 这是因为cordova的we ...
- HP ALM 使用经验
使用HP ALM(Application Lifecycle Management)软件有一个多月的时间了,我是从安装,部署,建项,配置,使用,再到问题收集,这个过程过来的.发现ALM是一个功能确实强 ...
- Issue 1:sigmod 撞车
11.6晚22:40,距离论文截止还有5天.在最后的紧要关头,竟然发现学术上撞车了,非常戏剧性的一幕,这么狗血的事情尽然就这么发生了. 自2015年8月份以来,本人一直在研究快速检查点领域.最近一篇t ...
- js 将json字符串转换为json对象的方法解析
推荐: var obj = eval('(' + str + ')'); var last=JSON.stringify(obj); //将JSON对象转化为string字符 例如: JSON字符串: ...
- vulcan测试记录
感觉这个游戏很赞,是六个里面最喜欢的一个了 1.有时候挖坑对于位置要求比较大? 2.感觉难度比较大,尤其是玩到第三关很考验啊(不过从另一个方面来说也是优点?) 3.玩到现在对于怪物吃金子的原理没有很懂 ...
- url传参中文乱码
当使用url重定向传参的时候,比如: javascript:window.location.href='modifyBook.jsp?BName=<%=URLEncoder.encode(&qu ...
- peoplesoft SQR language
Understanding SQR Data Elements !Variables!Variables are storage places for text or numbers that you ...
- 【C#】时间戳转换
今天有时间戳转换的需求,网上找了半天才找到相关代码,经测试有效,特作此笔记和大家分享! 1.时间戳转为C#格式时间 /// <summary> /// 时间戳转为C#格式时间 /// &l ...