• 并查集(按秩合并,路径压缩)


        并:Union:合并:合并两个不同的集合;
        查:Find:查找:判断两个集合是否在一个集合;
        集:Set:集合;

    1. /**
    2. 并:Union:合并:合并两个不同的集合;
    3. 查:Find:查找:判断两个集合是否在一个集合;
    4. 集:Set:集合;
    5. */
    6. /**
    7. #include
    8. using namespace std;
    9. #define MAXN 1000
    10. typedef int ElementType;
    11. typedef int SetName;
    12. typedef ElementType SetType[MAXN];
    13. SetName FindFather(SetType S,ElementType x); //查找x所属元素的集合;
    14. void Union(SetType S,SetName root1,SetName root2); //合并两个不同的集合
    15. int main()
    16. {
    17. int n;
    18. cin >> n;
    19. return 0;
    20. }
    21. SetName FindFather(SetType S,ElementType x); //查找x所属元素的集合;
    22. {
    23. while(S[x]>=0)
    24. x=S[x];
    25. return x;
    26. }
    27. void Union(SetType S,SetName root1,SetName root2); //合并两个不同的集合
    28. {
    29. root1=FindFather(S,root1);
    30. root2=FindFather(S,root2);
    31. if(root1!=root2)
    32. S[root1]=root2;
    33. }
    34. */

     2)按秩合并集合,对Union进行优化;

    1. /**
    2. 2)按秩合并集合,对Union进行优化;
    3. */
    4. /**
    5. 并:Union:合并:合并两个不同的集合;
    6. 查:Find:查找:判断两个集合是否在一个集合;
    7. 集:Set:集合;
    8. */
    9. /**
    10. #include
    11. using namespace std;
    12. #define MAXN 1000
    13. typedef int ElementType;
    14. typedef int SetName;
    15. typedef ElementType SetType[MAXN];
    16. SetName FindFather(SetType S,ElementType x); //查找x所属元素的集合;
    17. void Union(SetType S,SetName root1,SetName root2); //合并两个不同的集合
    18. int main()
    19. {
    20. int n;
    21. cin >> n;
    22. return 0;
    23. }
    24. SetName FindFather(SetType S,ElementType x) //查找x所属元素的集合;
    25. {
    26. while(S[x]>=0)
    27. x=S[x];
    28. return x;
    29. }
    30. void Union(SetType S,SetName root1,SetName root2) //合并两个不同的集合
    31. {
    32. root1=FindFather(S,root1);
    33. root2=FindFather(S,root2);
    34. if(root1!=root2)
    35. {
    36. if(S[root1]
    37. {
    38. S[root1]+=S[root2];
    39. S[root2]=root1; //将root2(矮树)挂在root1(高树)上;
    40. }
    41. else if(S[root2]
    42. {
    43. S[root2]+=S[root1];
    44. S[root1]=root2;
    45. }
    46. }
    47. }
    48. */

    3)路径压缩:将集合的每个子孩子的父亲节点都设置为根节点;
        以便为后面的Find函数减低查找时间;对Find进行优化;

    1. /**
    2. 3)路径压缩:将集合的每个子孩子的父亲节点都设置为根节点;
    3. 以便为后面的Find函数减低查找时间;对Find进行优化;
    4. */
    5. /**
    6. 并:Union:合并:合并两个不同的集合;
    7. 查:Find:查找:判断两个集合是否在一个集合;
    8. 集:Set:集合;
    9. */
    10. #include
    11. using namespace std;
    12. #define MAXN 1000
    13. typedef int ElementType;
    14. typedef int SetName;
    15. typedef ElementType SetType[MAXN];
    16. SetName FindFather(SetType S,ElementType x); //查找x所属元素的集合;
    17. void Union(SetType S,SetName root1,SetName root2); //合并两个不同的集合
    18. int main()
    19. {
    20. int n;
    21. cin >> n;
    22. return 0;
    23. }
    24. SetName FindFather(SetType S,ElementType x) //查找x所属元素的集合;
    25. {
    26. if(S[x]<0)
    27. return x;
    28. else
    29. return S[x]=Find(S,S[x]);
    30. }
    31. void Union(SetType S,SetName root1,SetName root2) //合并两个不同的集合
    32. {
    33. root1=FindFather(S,root1);
    34. root2=FindFather(S,root2);
    35. if(root1!=root2)
    36. {
    37. if(S[root1]//如果集合root1的元素要多一点
    38. {
    39. S[root1]+=S[root2];
    40. S[root2]=root1; //将root2(矮树)挂在root1(高树)上;
    41. }
    42. else if(S[root2]
    43. {
    44. S[root2]+=S[root1];
    45. S[root1]=root2;
    46. }
    47. }
    48. }

  • 相关阅读:
    Spark SQL 每年的1月1日算当年的第一个自然周, 给出日期,计算是本年的第几周
    《算法通关村第二关——终于学会链表反转了》
    Lucene数据写入流程
    LVGL自定义组件__页面指示器
    k8s集群Job Pod 容器可能因为多种原因失效,想要更加稳定的使用Job负载,有哪些需要注意的地方?
    风控知多点|模型是哪些内容出现了变动才需要重新迭代调整?
    遗传算法GA求解非连续函数问题
    HTTP状态码206报错
    C语言之文件操作
    Spring Boot到底是如何进行自动配置的?
  • 原文地址:https://blog.csdn.net/qq_51825761/article/details/126206159