当前位置:网站首页>路径压缩、、
路径压缩、、
2022-08-01 23:21:00 【Wanderer001】
并查集中的find函数,可以用于查找某个节点的父亲节点,某些情况下,我们为了加快查找的速度,就要用到路径压缩的写法。
int find(int x)
{
int tmp,son;
son=x;
while(x!=pra[x])
x=pra[x];
while(son!=x)
{
tmp=pra[son];
pra[son]=x;
son=tmp;
}
return x;
}我们一开始找到了x的父亲节点,之后,我们随着(X--->X的祖先)这条路,一直把这条路上的所有节点的父节点都标记为祖先节点。从而加快了查找的速度。
################################################################
有关并查集join函数的新认识:
1.如果不考虑树的整体结构,
void join(int b1,int b2)//两颗树的合并操作
{
int x=find(b1);//找到树b1的根节点
int y=find(b2);//找到树b2的根节点
if(x!=y)//如果两棵树不同根
pra[y]=x;//链接两棵树
}2.需要考虑到树的整体结构:
void join(int b1,int b2)//两颗树的合并操作
{
int x=find(b1);//找到树b1的根节点
int y=find(b2);//找到树b2的根节点
if(x!=y)//如果两棵树不同根
pra[b2]=b1;//链接两棵树
}其他东西:
1.FIND函数能查找子节点的祖先节点
2.采用了路径压缩的FIND函数、JOIN函数可以修改子节点的父亲节点。
######################################################
路径压缩的使用前提是不考虑树的整体结构,也就是说FIND函数必须采用第一种写法。
边栏推荐
猜你喜欢

【数据分析03】
![Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights](/img/f1/9824f32dd4fe4b3e94af3f945b1801.png)
Thesis understanding [RL - Exp Replay] - Experience Replay with Likelihood-free Importance Weights

系统可用性:SRE口中的3个9,4个9...到底是个什么东西?

The monthly salary of the test post is 5-9k, how to increase the salary to 25k?

Solve the port to take up

Secondary Vocational Network Security Competition B7 Competition Deployment Process

img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)

PDF转Word有那么难吗?做一个文件转换器,都解决了

分享10套开源免费的高品质源码,免费源码下载平台

华为无线设备配置全局双链路冷备份(AC全局配置方式)
随机推荐
npm包【详解】(内含npm包的开发、发布、安装、更新、搜索、卸载、查看、版本号更新规则、package.json详解等)
[C language advanced] file operation (2)
最短路模板
img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)
Calculate the midpoint between two points
[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环
测试岗月薪5-9k,如何实现涨薪到25k?
云原生DevOps环境搭建
excel remove all carriage return from a cell
excel remove all carriage return from a cell
【C语言进阶】文件操作(二)
中职网络安全竞赛B7比赛部署流程
PDF转Word有那么难吗?做一个文件转换器,都解决了
检查点是否在矩形内
分享10套开源免费的高品质源码,免费源码下载平台
【参营经历贴】2022网安夏令营
When using DocumentFragments add a large number of elements
【C补充】链表专题 - 单向链表
cmd command
如何更好的理解的和做好工作?