• 并查集的实现【学习算法】


    前言

    2023-9-26 14:38:02

    以下内容源自《【学习算法】》
    仅供学习交流使用

    版权

    禁止其他平台发布时删除以下此话
    本文首次发布于CSDN平台
    作者是CSDN@日星月云
    博客主页是https://blog.csdn.net/qq_51625007
    禁止其他平台发布时删除以上此话

    推荐

    算法讲解056【必备】并查集-上

    并查集的实现

    并查集的实现

    package test1;// 并查集模版(牛客)
    // 路径压缩 + 小挂大
    // 测试链接 : https://www.nowcoder.com/practice/e7ed657974934a30b2010046536a5372
    // 请同学们务必参考如下代码中关于输入、输出的处理
    // 这是输入输出处理效率很高的写法
    // 提交以下的code,提交时请把类名改成"Main",可以直接通过
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.StreamTokenizer;
    class Code01_UnionFindNowCoder {
    
        public static int MAXN = 1000001;
    
        public static int[] father = new int[MAXN];
    
        public static int[] size = new int[MAXN];
    
        public static int[] stack = new int[MAXN];
    
        public static int n;
    
        public static void build() {
            for (int i = 0; i <= n; i++) {
                father[i] = i;
                size[i] = 1;
            }
        }
    
        // i号节点,往上一直找,找到代表节点返回!
        public static int find(int i) {
            // 沿途收集了几个点
            int size = 0;
            while (i != father[i]) {
                stack[size++] = i;
                i = father[i];
            }
            // 沿途节点收集好了,i已经跳到代表节点了
            while (size > 0) {
                father[stack[--size]] = i;
            }
            return i;
        }
    
        public static boolean isSameSet(int x, int y) {
            return find(x) == find(y);
        }
    
        public static void union(int x, int y) {
            int fx = find(x);
            int fy = find(y);
            if (fx != fy) {
                // fx是集合的代表:拿大小
                // fy是集合的代表:拿大小
                if (size[fx] >= size[fy]) {
                    size[fx] += size[fy];
                    father[fy] = fx;
                } else {
                    size[fy] += size[fx];
                    father[fx] = fy;
                }
            }
        }
    
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            StreamTokenizer in = new StreamTokenizer(br);
            PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
            while (in.nextToken() != StreamTokenizer.TT_EOF) {
                n = (int) in.nval;
                build();
                in.nextToken();
                int m = (int) in.nval;
                for (int i = 0; i < m; i++) {
                    in.nextToken();
                    int op = (int) in.nval;
                    in.nextToken();
                    int x = (int) in.nval;
                    in.nextToken();
                    int y = (int) in.nval;
                    if (op == 1) {
                        out.println(isSameSet(x, y) ? "Yes" : "No");
                    } else {
                        union(x, y);
                    }
                }
            }
            out.flush();
            out.close();
            br.close();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96

    最后

    我们都有光明的未来

    祝大家考研上岸
    祝大家工作顺利
    祝大家得偿所愿
    祝大家如愿以偿
    点赞收藏关注哦

  • 相关阅读:
    Vue组件懒加载
    制药企业固体制剂设备管理及维护要点
    【温故而知新】构建高可用Linux服务器(一)
    aspnetcore中aop的实现
    20000字深度讲解 Python 数据可视化神器 Plotly
    虚幻引擎 5.1 中全新的增强型输入操作系统
    Cordova插件开发:集成南方测绘RTK实现高精度卫星定位
    福云玩具销售系统
    解决用IPV6+DDNS访问UNRAID webui周期性失效的问题,smb不能访问的问题
    MarkDown+Hbuilder学习
  • 原文地址:https://blog.csdn.net/qq_51625007/article/details/133309133