并:Union:合并:合并两个不同的集合;
查:Find:查找:判断两个集合是否在一个集合;
集:Set:集合;
- /**
- 并:Union:合并:合并两个不同的集合;
- 查:Find:查找:判断两个集合是否在一个集合;
- 集:Set:集合;
- */
-
- /**
- #include
-
- using namespace std;
-
- #define MAXN 1000
- typedef int ElementType;
- typedef int SetName;
- typedef ElementType SetType[MAXN];
-
- SetName FindFather(SetType S,ElementType x); //查找x所属元素的集合;
- void Union(SetType S,SetName root1,SetName root2); //合并两个不同的集合
-
- int main()
- {
- int n;
- cin >> n;
-
- return 0;
- }
-
- SetName FindFather(SetType S,ElementType x); //查找x所属元素的集合;
- {
- while(S[x]>=0)
- x=S[x];
- return x;
- }
-
- void Union(SetType S,SetName root1,SetName root2); //合并两个不同的集合
- {
- root1=FindFather(S,root1);
- root2=FindFather(S,root2);
- if(root1!=root2)
- S[root1]=root2;
- }
- */
2)按秩合并集合,对Union进行优化;
- /**
- 2)按秩合并集合,对Union进行优化;
- */
-
- /**
- 并:Union:合并:合并两个不同的集合;
- 查:Find:查找:判断两个集合是否在一个集合;
- 集:Set:集合;
- */
-
- /**
- #include
-
- using namespace std;
-
- #define MAXN 1000
- typedef int ElementType;
- typedef int SetName;
- typedef ElementType SetType[MAXN];
-
- SetName FindFather(SetType S,ElementType x); //查找x所属元素的集合;
- void Union(SetType S,SetName root1,SetName root2); //合并两个不同的集合
-
- int main()
- {
- int n;
- cin >> n;
-
- return 0;
- }
-
- SetName FindFather(SetType S,ElementType x) //查找x所属元素的集合;
- {
- while(S[x]>=0)
- x=S[x];
- return x;
- }
-
- void Union(SetType S,SetName root1,SetName root2) //合并两个不同的集合
- {
- root1=FindFather(S,root1);
- root2=FindFather(S,root2);
- if(root1!=root2)
- {
- if(S[root1]
- {
- S[root1]+=S[root2];
- S[root2]=root1; //将root2(矮树)挂在root1(高树)上;
- }
- else if(S[root2]
- {
- S[root2]+=S[root1];
- S[root1]=root2;
- }
- }
- }
- */
3)路径压缩:将集合的每个子孩子的父亲节点都设置为根节点;
以便为后面的Find函数减低查找时间;对Find进行优化;
-
- /**
- 3)路径压缩:将集合的每个子孩子的父亲节点都设置为根节点;
- 以便为后面的Find函数减低查找时间;对Find进行优化;
- */
-
- /**
- 并:Union:合并:合并两个不同的集合;
- 查:Find:查找:判断两个集合是否在一个集合;
- 集:Set:集合;
- */
-
-
- #include
-
- using namespace std;
-
- #define MAXN 1000
- typedef int ElementType;
- typedef int SetName;
- typedef ElementType SetType[MAXN];
-
- SetName FindFather(SetType S,ElementType x); //查找x所属元素的集合;
- void Union(SetType S,SetName root1,SetName root2); //合并两个不同的集合
-
- int main()
- {
- int n;
- cin >> n;
-
- return 0;
- }
-
- SetName FindFather(SetType S,ElementType x) //查找x所属元素的集合;
- {
- if(S[x]<0)
- return x;
- else
- return S[x]=Find(S,S[x]);
- }
-
- void Union(SetType S,SetName root1,SetName root2) //合并两个不同的集合
- {
- root1=FindFather(S,root1);
- root2=FindFather(S,root2);
- if(root1!=root2)
- {
- if(S[root1]
//如果集合root1的元素要多一点 - {
- S[root1]+=S[root2];
- S[root2]=root1; //将root2(矮树)挂在root1(高树)上;
- }
- else if(S[root2]
- {
- S[root2]+=S[root1];
- S[root1]=root2;
- }
- }
- }
-
相关阅读:
RuntimeError: cuDNN error: CUDNN_STATUS_INTERNAL_ERROR
给你两个集合,要求{A} + {B}
矩阵分析与应用+张贤达
MySQL-索引优化/查询优化
ES6如何声明一个类?类如何继承?
代码随想录算法训练营第四十四天 | 416. 分割等和子集
Kotlin协程
2023最新的接口自动化测试完整版
java.net.UnknownHostException: eureka
国外无人机蜂群作战样式进展及反蜂群策略研究
-
原文地址:https://blog.csdn.net/qq_51825761/article/details/126206159
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU