![](https://img-blog.csdnimg.cn/20201014180756780.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
并查集
并查集
柒白_
这个作者很懒,什么都没留下…
展开
-
网络分析(并查集 + 差分)
2069. 网络分析网络分析题意:动态维护集合的合并和集合内元素的查询 思路:1.类似区间合并单点查询,可以构造子节点和父节点直接的差分,即子节点真实值为该节点的值加上父节点的值,。2.由于并查集的路径压缩,每一次路径压缩都需要加上当前父节点的值例如如图1.当将集合4合并到集合1之下时候,为满足差分保证5,6不变,w[4]−=w[1]w[4] -= w[1]w[4]−=w[1].2.当将5-4-1的路径压缩程5-1时,w[5]+=w[4]w[5] += w[4]w[5]+=w[4]原创 2022-04-05 20:32:11 · 263 阅读 · 0 评论 -
修改数组(并查集)
1242. 修改数组(并查集)修改数组 样例输入:52 1 1 3 4 样例输出:2 1 3 4 5 代码:#include <cstdio>const int N = 1100010;int p[N];int find(int x){ if (p[x] != x) p[x] = find(p[x]); return p[x];}int main(){ int n; scanf("%d", &n); f原创 2022-04-04 19:58:42 · 71 阅读 · 0 评论 -
acwing食物链(带权并查集)
食物链输入样例:100 71 101 1 2 1 22 2 3 2 3 3 1 1 3 2 3 1 1 5 5输出样例:3模板:#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 50005;int n, m;int p[N], d[N];//0 表示与根原创 2022-03-21 11:05:28 · 104 阅读 · 0 评论 -
acwing连通块中点得数量
连通块中点的数量输入样例:5 5C 1 2Q1 1 2Q2 1C 2 5Q2 5输出样例:Yes23模板://连通块中点的数量#include<iostream>using namespace std;const int N = 100010;int p[N],size[N];int n,m; int find(int x){ if(p[x]!=x) p[x]=find(p[x]); return p[x];}int main(){ scan原创 2022-03-21 11:04:08 · 109 阅读 · 0 评论 -
acwing合并集合
合并集合输入样例:4 5M 1 2M 3 4Q 1 2Q 1 3Q 3 4输出样例:YesNoYes模板://并查集//1.讲两个集合合并 2.询问两个元素是否再一个集合当中 // 如何判断树根// 如何求x的集合编号// 如何合并两个集合 px是x的集合编号 py是y的集合编号 。 #include<iostream>using namespace std;const int N = 100010;int n,m;int p[N];int f原创 2022-03-21 11:02:41 · 78 阅读 · 0 评论