并查集initial,find,union+应用

initial:

cpp 复制代码
void initial(int n) {
    for (int i = 0; i < n; i++) {
        p[i] = i;
        h[i] = 1;
    }
}

find:

cpp 复制代码
int find(int x) {
    if (p[x] == x) return x;
    else return p[x] = find(p[x]);
}

union:

cpp 复制代码
void union(int x, int y) {
    int rootx = find(x);
    int rooty = find(y);
    if (rootx != rooty) {
        if (h[rootx] < h[rooty]) p[rootx] = rooty;
        else if (h[rootx] > h[rooty]) p[rooty] = rootx;
        else p[rootx] = rooty, h[rooty]++;
    }
}

例题:

cpp 复制代码
#include <iostream>
using namespace std;

const int N = 10010;

int p[N], h[N];
bool visited[N];         // 记录鸟是否出现过
bool isroot[N];          // 记录某个根节点是否已被计入树
int photos[10000][10];   // 每张照片中最多10只鸟,最多10000张照片
int photosize[10000];    // 每张照片的鸟的数量

void initial(int n) {
    for (int i = 0; i < n; i++) {
        p[i] = i;
        h[i] = 1;
        visited[i] = false;
        isroot[i] = false;
    }
}

int find(int x) {
    if (p[x] == x) return x;
    else return p[x] = find(p[x]);
}

void unionset(int x, int y) {
    int rootx = find(x);
    int rooty = find(y);
    if (rootx != rooty) {
        if (h[rootx] < h[rooty]) p[rootx] = rooty;
        else if (h[rootx] > h[rooty]) p[rooty] = rootx;
        else p[rootx] = rooty, h[rooty]++;
    }
}

int main() {
    int n;
    cin >> n;

    initial(N);

    int maxid = 0;

    // 输入照片数据
    for (int i = 0; i < n; i++) {
        int k;
        cin >> k;
        photosize[i] = k;
        for (int j = 0; j < k; j++) {
            int bird;
            cin >> bird;
            photos[i][j] = bird;
            visited[bird] = true;
            if (bird > maxid) maxid = bird;
        }
    }

    // 合并同一张照片的鸟
    for (int i = 0; i < n; i++) {
        int k = photosize[i];
        for (int j = 1; j < k; j++) {
            unionset(photos[i][0], photos[i][j]);
        }
    }

    int birdcnt = 0, treecnt = 0;

    // 统计鸟的数量和树的数量(去重根节点)
    for (int i = 1; i <= maxid; i++) {
        if (visited[i]) {
            birdcnt++;
            int root = find(i);
            if (!isroot[root]) {
                isroot[root] = true;
                treecnt++;
            }
        }
    }

    cout << treecnt << " " << birdcnt << endl;

    // 处理查询
    int q;
    cin >> q;
    while (q--) {
        int x, y;
        cin >> x >> y;
        if (find(x) == find(y)) cout << "Yes" << endl;
        else cout << "No" << endl;
    }

    return 0;
}
相关推荐
ysa0510303 分钟前
竞赛常用加速技巧#模板
c++·笔记·算法
7 976 分钟前
C语言基础知识--文件的顺序读写与随机读写
java·数据结构·算法
2401_841003986 分钟前
Kubernetes 资源管理全解析
算法·贪心算法
☆璇1 小时前
【数据结构】排序
c语言·开发语言·数据结构·算法·排序算法
艾莉丝努力练剑4 小时前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
_殊途6 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
珊瑚里的鱼9 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
秋说10 小时前
【PTA数据结构 | C语言版】顺序队列的3个操作
c语言·数据结构·算法
lifallen10 小时前
Kafka 时间轮深度解析:如何O(1)处理定时任务
java·数据结构·分布式·后端·算法·kafka
liupenglove10 小时前
自动驾驶数据仓库:时间片合并算法。
大数据·数据仓库·算法·elasticsearch·自动驾驶