首页 > 其他分享 >森林与并查集

森林与并查集

时间:2022-10-15 23:11:58浏览次数:61  
标签:set return int 查集 flag UnionSet find 森林

相关算法 合并的时间复杂度 查找的时间复杂度
Quick-Find算法 O(N) O(1)
Quick-Union算法 O(lgN)~O(N) O(lgN)~O(N)
Weighted按质优化 O(lgN) O(lgN)
路径压缩 ≈O(1) ≈O(1)

在实际工程中,可直接跳过按质优化采用路径压缩,时间效率上相差不大,但可以节省存储节点个数 (weight) 的内存消耗

Quick-Find

#include <stdio.h>
#include <stdlib.h>

typedef struct UnionSet {
    int *flag;
    int n;
} UnionSet;

UnionSet *init(int n) {
    UnionSet *set = (UnionSet *)malloc(sizeof(UnionSet));
    set->flag = (int *)malloc(sizeof(int) * (n + 1)); //编号一般从1开始
    for (int i = 1; i <= n; i++) {
        set->flag[i] = i;
    }
    set->n = n;
    return set;
}

void clear(UnionSet *set) {
    if (set == NULL) return;
    free(set->flag);
    free(set);
    return;
}

int find(UnionSet *set, int a) {
    return set->flag[a];
}

int merge(UnionSet *set, int a, int b) {
    if (find(set, a) == find(set, b)) return 0;
    int flag_b = set->flag[b];
    for (int i = 1; i <= set->n; i++) {
        if (set->flag[i] == flag_b) set->flag[i] = set->flag[a];
    }
    return 1;
}

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    UnionSet *set = init(n);
    for (int i = 0; i < m; i++) {
        int op, a, b;
        scanf("%d%d%d", &op, &a, &b);
        switch (op) {
            case 1:
                merge(set, a, b);
                break;
            case 2:
                printf("%s\n", find(set, a) == find(set, b) ? "YES" : "NO");
                break;
        }
    }
    clear(set);
    return 0;
}

Quick-Union

#include <stdio.h>
#include <stdlib.h>

typedef struct UnionSet {
    int *flag; //记录父节点的编号
    int n;
} UnionSet;

UnionSet *init(int n) {
    UnionSet *set = (UnionSet *)malloc(sizeof(UnionSet));
    set->flag = (int *)malloc(sizeof(int) * (n + 1)); //编号一般从1开始
    for (int i = 1; i <= n; i++) {
        set->flag[i] = i;
    }
    set->n = n;
    return set;
}

void clear(UnionSet *set) {
    if (set == NULL) return;
    free(set->flag);
    free(set);
    return;
}

int find(UnionSet *set, int a) {
    if (set->flag[a] == a) return a;
    return find(set, set->flag[a]);
    //return set->flag[a] = find(set, set->flag[a]); //路径压缩
}

int merge(UnionSet *set, int a, int b) {
    int flag_a = find(set, a);
    int flag_b = find(set, b);
    if (flag_a == flag_b) return 0;
    set->flag[flag_b] = flag_a;
    return 1;
}

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    UnionSet *set = init(n);
    for (int i = 0; i < m; i++) {
        int op, a, b;
        scanf("%d%d%d", &op, &a, &b);
        switch (op) {
            case 1:
                merge(set, a, b);
                break;
            case 2:
                printf("%s\n", find(set, a) == find(set, b) ? "YES" : "NO");
                break;
        }
    }
    clear(set);
    return 0;
}

Weighted Quick-Union

#include <stdio.h>
#include <stdlib.h>

#define swap(a, b) { \
    __typeof(a) __temp = a; \
    a = b, b = __temp; \
}

typedef struct UnionSet {
    int *flag; //记录父节点的编号
    int *node_num; //记录子集(包括自身)的节点数量
    int n;
} UnionSet;

UnionSet *init(int n) {
    UnionSet *set = (UnionSet *)malloc(sizeof(UnionSet));
    set->flag = (int *)malloc(sizeof(int) * (n + 1)); //编号一般从1开始
    set->node_num = (int *)malloc(sizeof(int) * (n + 1));
    for (int i = 1; i <= n; i++) {
        set->flag[i] = i;
        set->node_num[i] = 1;
    }
    set->n = n;
    return set;
}

void clear(UnionSet *set) {
    if (set == NULL) return;
    free(set->flag);
    free(set);
    return;
}

int find(UnionSet *set, int a) {
    if (set->flag[a] == a) return a;
    return find(set, set->flag[a]);
    //return set->flag[a] = find(set, set->flag[a]); //路径压缩
}

int merge(UnionSet *set, int a, int b) {
    int fa = find(set, a);
    int fb = find(set, b);
    if (fa == fb) return 0;
    if (set->node_num[fb] > set->node_num[fa]) swap(fa, fb);
    set->flag[fb] = fa;
    set->node_num[fa] += set->node_num[fb];
    return 1;
}

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    UnionSet *set = init(n);
    for (int i = 0; i < m; i++) {
        int op, a, b;
        scanf("%d%d%d", &op, &a, &b);
        switch (op) {
            case 1:
                merge(set, a, b);
                break;
            case 2:
                printf("%s\n", find(set, a) == find(set, b) ? "YES" : "NO");
                break;
        }
    }
    clear(set);
    return 0;
}

可采用OJ上第71题的“朋友圈”进行测试

标签:set,return,int,查集,flag,UnionSet,find,森林
From: https://www.cnblogs.com/Kelvin-Wu/p/16795309.html

相关文章

  • Codeforces Round #747 (Div. 2) D // 扩展域并查集
    题目来源:CodeforcesRound#747(Div.2)D-TheNumberofImposters题目链接:Problem-D-Codeforces题意有\(n\)个人,每个人拥有\(imposter\)或\(crewmate\)的身份......
  • Boosting, Bootstrap, Adaboost, GBDT, XGBoost, 随机森林
     Adaboost,按分类对错,分配不同的weight,计算cost function时使用这些weight,从而让“错分的样本权重越来越大,使它们更被重视”。Bootstrap也有类似思想,它在每一步迭代时不......
  • 洛谷 P2387 [NOI2014] 魔法森林 题解【动态加点 SPFA】
    题目大意给定一个由\(n\)个点\(m\)条边的无向图,每条边有权值\((a,b)\),求一条路径使这条路径上的\((a_{\max}+b_{\max})\)最小。思路正解应该是LCT动态维护MST......
  • P3402 可持久化并查集
    P3402通过主席树维护不同版本的并查集,注意要采用按秩合并的方式,路径压缩可能会爆。1#include<bits/stdc++.h>2usingnamespacestd;3constintN=3e5+10;......
  • 二叉树与树、森林之间的转换
    一、成树、森林转换为二叉树树转化成二叉树的步骤:树中所有相邻兄弟结点之间加一条线对树中的每个结点只保留它与长子之间的连线,删除与其他孩子之间的连线以树的根结点......
  • 并查集应用 matlab
    ​有lun文和源码群 1160391469并查集是一种用来管理分组情况的数据结构,可以高效的查询元素a,元素b是否来自于一组。并且可以合并元素a和元素b所在的组。虽然并查集可以......
  • CF1681F. Unique Occurrences (可撤销并查集, 分治)
    https://codeforces.com/contest/1681/problem/F题意:给5e5节点的树,问所有路径的贡献和,一条路径的贡献指路径上上只出现一次的边权的个数。思路:对于每种边权的贡献:对于边......
  • LeetCode 15 - 并查集
    所谓并查集,是指支持集合的合并和查询操作的数据结构。合并:将两个集合合并为一个。查询:查询某个元素属于哪个集合,通常是返回集合内的一个代表元素(例如二叉树的所有结点可......
  • ARC149D Simultaneous Sugoroku(并查集)
    ARC149DSimultaneousSugoroku有\(N\)个数\(X_i\)和\(M\)个数\(D_i\),对每个\(X_i\)询问依次对\(j=1\ton\)执行:如果\(X_i>0\)就\(-D_j\),如果\(X_i<......
  • 森林里住着一只老虎。一天,它饿
    http://ds.163.com/article/63376ea21c09770001f18cac/http://ds.163.com/feed/63376ea21c09770001f18cac/http://ds.163.com/article/63376ea4c5e20100011e47a8/http://ds.......