数据结构--并查集--基础

本文介绍了并查集这一数据结构,包括其定义、合并两个集合的操作、查询点所在集合的方法,以及针对不同场景(如连通块中的点数量、食物链问题)的优化策略。通过代码示例展示了如何在实际问题中应用并查集。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

并查集

简介

定义

用于快速合并和查询集合的数据结构

操作

  • 合并两个集合
  • 查询某个点在哪个集合

优化

  • 路径压缩
    • 每次在查询一个节点时,直接将该节点指向集合根节点
  • 按秩合并
    • 将集合中元素数量少的接在元素数量多的集合中

模板

836. 合并集合

#include <iostream>

using namespace std;

const int N = 1e5 + 10;

int n, m;
int p[N];

// 寻找x的祖宗节点
int find(int x)
{
    if(x != p[x]) p[x] = find(p[x]);
    return p[x];
}

int main()
{
    cin >> n >> m;
    
    
    // 初始化并查集
    for(int i = 0; i < n; i ++)
    {
        p[i] = i;
    }
    
    while(m -- )
    {
        char op[2];
        int a, b;
        
        cin >> op >> a >> b;
        
        if(op[0] == 'M')
        {
            // 合并两个集合
            p[find(a)] = find(b);
        } else {
            // 查询两个数是否在同一个集合中
            if(find(a) == find(b)) puts("Yes");
            else puts("No");
        }
    }
    
    return 0;
}

应用

837. 连通块中点的数量

分析

本题中只有询问集合数量是特别引入,其他与模板相同

  • 额外记录集合元素数量
  • 将元素数量与根节点绑定,更新时,只需要用根节点的数量加上新加入元素的节点
代码
#include <iostream>

using namespace std;

const int N = 1e5 + 10;

int n, m;
int p[N], s[N];

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


int main()
{
    cin >> n >> m;
    
    for(int i = 0; i < n; i ++)
    {
        p[i] = i;
        s[i] = 1;
    }
    
    while(m -- )
    {
        string op;
        int a, b;
        cin >> op;
        
        if(op == "Q1")
        {
            // 两个元素是否在一个集合中
            cin >> a >> b;
            if(find(a) == find(b)) puts("Yes");
            else puts("No");
            
        } else if(op == "Q2")
        {
            // 询问集合的元素数量
            cin >> a;
            printf("%d\n", s[find(a)]);
            
        } else {
            // 合并集合
            cin >> a >> b;
            if(find(a) == find(b)) continue;
            s[find(b)] += s[find(a)];
            p[find(a)] = find(b);
        }
    }
    
    
    return 0;
}

240. 食物链

分析

本题中引入了新的元素距离,其他与模板相同

  • 将元素距离根节点的距离元素节点绑定
代码
#include<iostream>

using namespace std;

const int N = 50010;

int n, k;
int p[N], d[N];

int find(int x)
{
     if(x != p[x])
     {
         int t = find(p[x]);
         d[x] += d[p[x]];
         p[x] = t;
     }
     
     return p[x];
}

int main()
{
    
    cin >> n >> k;
    
    // 初始化并查集
    for(int i = 1 ; i <= n ;i ++ ) p[i] = i;
    
    
    int res = 0; // 表示假话的数量
    while(k -- )
    {
        // 处理询问
        int op, x, y;
        cin >> op >>x >> y;
        
        if(x > n || y > n) res ++ ;
        else {
            int px = find(x), py = find(y);
            if(op == 1)
            {
                // x 与 y为同类
                if(px == py && (d[x] - d[y]) % 3) res ++ ;
                else if(px != py)
                {
                    p[px] = py;
                    d[px] = d[y] - d[x];
                }
            } else {
                // x吃y dx 比dy多一
                
                if(px == py && (d[x] - d[y] - 1) % 3) res ++ ;
                else if(px != py)
                {
                    p[px] = py;
                    d[px] = d[y] - d[x] + 1;
                }
            }
        }
    }
    
    cout << res << endl;
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wbzuo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值