![](https://img-blog.csdnimg.cn/20201014180756919.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
并查集与带权并查集
易数
这个作者很懒,什么都没留下…
展开
-
【codevs】1503 愚蠢的宠物
题解: #include #include #include using namespace std; #define maxn 1000000+100 int father[maxn]; int a[maxn],b[maxn]; int main() { int n,x,y; memset(a,0x3f,sizeof(a)); memset(b,0x3f,sizeof(b));原创 2016-06-19 20:24:29 · 318 阅读 · 0 评论 -
【codevs】1073 家族
题解 #include #include using namespace std; int father[5000+100]; void init(int n) { for(int i=1;i<=n;i++) father[i]=i; } int find(int x) { if(x!=father[x]) father[x]=find(father[x]); return f原创 2016-06-19 21:12:18 · 283 阅读 · 0 评论 -
【codevs】1074 食物链
题解 题目要求我们判假,如果我们用并查集来做这道题,应该何从下手呢?首先,很显然的是,我们必须维护一个点与点关系的集合,通过这个关系集合来判假。原始的并查集只能用于维护“属于同一类“的这种关系,但是这道题却还要求我们维护吃的关系,这样才能针对每一个给出的论断进行判断。维护属于同一组的这种信息很容易实现,但是又如何体现捕食的关系呢?稍加思考,可以这样来转化。针对每只动物,为他们分别创建三个元素原创 2016-06-18 14:15:58 · 535 阅读 · 0 评论 -
【codevs】1069 关押罪犯
题解 简单#include #include #include using namespace std; int n,m,father[40100],x,y; struct data{ int a,b,c; }e[100001]; int compar(const data &a,const data &b) { return a.c>b.c; } void init(in原创 2016-06-19 09:32:07 · 415 阅读 · 0 评论 -
【rqnoj】PID343 mty的考验
题解 #include #include #include using namespace std; int father[1000+10]; int a[1000+10]; void init(int n) { for(int i=1;i<=n;i++) father[i]=i; } int find(int x) { if(x!=father[x]) father[x]=fi原创 2016-06-19 21:30:08 · 356 阅读 · 0 评论 -
【vijos】P1022 Victoria的舞会2
题解 #include #include using namespace std; int father[300]; int a[300]; void init(int n) { for(int i=1;i<=n;i++) father[i]=i; } int find(int x) { if(x!=father[x]) father[x]=find(father[x]); re原创 2016-06-19 21:53:37 · 483 阅读 · 0 评论 -
【codevs】2639 约会计划
题解: 简单的并查集应用。原创 2016-06-18 09:23:46 · 339 阅读 · 0 评论 -
【codevs1540】 银河英雄传说
题解 我们可以把战场的每一列看作一个集合,初始时每个集合中只有一个元素。M指令可以看作合并集合,这令我们想到并查集。定义father数组,s数组和sum数组,分别表示并查集中每个元素的父亲,距自己的父亲的距离和该集合中的元素个数。只需要合并和路径压缩的时候注意一下就可以了。C指令可以利用类似前缀和的思想。原创 2016-06-20 17:23:44 · 592 阅读 · 0 评论 -
【codevs 1001】 【bzoj1050】舒适的路线
题解: 1.边按权值排序,标号1~m 2.初始化一个枚举起点sta=1 3.初始化并查集 4.从sta开始顺推,利用并查集加边,直到s与t连通 5.记录当前边编号为r 6.初始化并查集 7.从r逆推,利用并查集加边,直到s与t连通 8.得到当前边编号,记为l 9.[l,r]是一组比较优的可行解,更新答案 10.枚举起点sta变为l+1,返回第3步继续执行原创 2016-06-20 18:02:47 · 955 阅读 · 0 评论