
并查集
圣帝天龙
菜!刷题啊!刷题不会,看题解啊!看题解看不懂,那就只能唱凉凉了
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
POJ1182带权并查集
(1).如果re[x]始终表示x到根之间的关系,具体分为:0表示和根同类,1表示吃根,2表示被根吃那么我们可以确定a,b之间的关系:(re[a]-re[b])%3表示a节点到b节点的关系用于判断逻辑错误和正确(2).那么如果用re[x]表示x到父节点pre[x]的关系我们可以通过(1)中任意两个节点的关系推得:当前节点到根节点的关系:re[x]=(re[pre[x]]+re[x]+...)...原创 2018-08-03 18:36:02 · 153 阅读 · 0 评论 -
HDU1856(并查集求最大集合)
有比较说明一点经验:求最大值有时候不需要把数据求出来后遍历。注意:最大值的求解和比较的顺序没有关系,所以可以一边求出数据,一边进行比较。【即用设置全局变量maxp的方法解决】省去最后遍历的步骤。 #include<iostream>#include<cstdio>#include<algorithm>#include<cstring&g...原创 2018-10-10 13:55:07 · 539 阅读 · 0 评论 -
【洛谷】P1197 [JSOI2008]星球大战
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<stack>using namespace std;const int maxn=400000+5;vector<...原创 2018-10-05 11:18:37 · 316 阅读 · 0 评论 -
POJ2492(带权并查集)
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;const int maxn=2005;int pre[maxn];int re[maxn];int n,m,t,T,p,q;bool flag;vo...原创 2018-09-08 23:11:12 · 181 阅读 · 0 评论 -
HDU3038带权并查集
看大佬的博客理解了,疑问的地方有两点,一:程序中的距离转换怎么用和根节点的距离来解释? 二:合并时为什么a--或者b++?直接合并a,b不行吗?这个问题我已经理解了,下面是我的思路:问题一的思路如图:程序中a,b,x,y四个节点之间距离关系的转换问题二: 为什么不能在unions()函数中直接传入a和b?答案是不能的:需要a--或b++最后,注意程序的连续输入:while(...原创 2018-08-02 19:27:21 · 1101 阅读 · 5 评论 -
HDU1213
节点的合并问题,注意输出格式,下面是AC代码: #include<iostream>#include<cstring>using namespace std;const int maxn=1005;int n,m,t;int root;//开始时桌子的数量int pre[maxn];int pp,qq;//当前可能合并的两个结点void make_se...原创 2018-08-02 10:21:15 · 193 阅读 · 0 评论 -
POJ1611
在并查集合并节点时,合并操作直接作用在当前节点的根结点上,和当前节点基本无关.下面是AC代码: #include<iostream>#include<cstring>using namespace std;const int maxn=30005;int n,m,k;int num[maxn];//num储存当前节点的编号int pre[maxn];...原创 2018-08-02 09:49:49 · 320 阅读 · 0 评论 -
POJ2236
时间限制10000ms,用了8844msAC,时间复杂度依然比较高.题目特点是在电脑修复前不能被其他电脑连接(即使在距离范围内也不允许),因此在节点中设置了good变量用来标记是否被修复,并查集: #include<iostream>#include<cstring>#include<cstdio>using namespace std;cons...原创 2018-08-01 21:23:52 · 745 阅读 · 0 评论 -
SDNU1016矩形合并(并查集)
几点教训:(1)将数据输入完成后再进行合并(2)两个矩形有交点的函数如果用必然有一个点的坐标在另一个矩形内这个原理来写,一共有8种情况,不能遗漏,下面是AC代码:#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn=105;s...原创 2018-08-03 21:24:13 · 711 阅读 · 0 评论 -
HDU1232畅通工程(经典并查集)
将至少要修的道路初始化为n-1:连通n个点最少需要n-1条边.判断当前两个节点pp和qq是不是在同一个集合中(并查集的功能:查找可以做到),如果不在同一个集合,连通两个集合,相当于修建一条路, cnt--;如果在一个集合中,不对它处理即可#include<cstdio>#include<iostream>using namespace std;const int...原创 2018-08-03 19:09:16 · 1981 阅读 · 0 评论 -
CodeForces - 566D(并查集区间合并更新优化)
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=2e5+10;int pre[maxn];int nex[maxn];int n,q;int foot1,foot2;i...原创 2018-11-19 22:16:01 · 325 阅读 · 0 评论