带权并查集 记录节点到根的距离

AcWing 240. 食物链

#include <bits/stdc++.h>
using namespace std;

const int N = 50005;

int p[N], dis[N];   //dis为到根节点的距离

int find(int x) {
    if (x != p[x]) {
        int former_p = p[x];
        p[x] = find(p[x]);
        dis[x] += dis[former_p];    //更新到根节点的距离
    }

    return p[x];
}

int main() 
{
#ifndef ONLINE_JUDGE
    freopen("D:/VS CODE/C++/in.txt", "r", stdin);
    freopen("D:/VS CODE/C++/out.txt", "w", stdout);
#endif
    int n, k;
    int ans = 0;
    cin >> n >> k;

    for (int i = 1; i <= n; ++i) {
        p[i] = i;
    }

    while (k -- ) {
        int c, x, y;
        cin >> c >> x >> y;

        if (x > n or y > n) ++ans;
        else if (c == 1) {
            //x y 同类
            int r_x = find(x), r_y = find(y);
            if (r_x != r_y) {
                //x 和 y之间的关系还不明确
                p[r_x] = r_y;
                dis[r_x] = dis[y] - dis[x]; 
                //(dis[r_x] + dis[x] - dis[y]) % 3 = 0;
            }
            else {
                if ((dis[x] - dis[y]) % 3) {
                    //不为同类
                    ++ans;
                }
            }
        }
        else {
            int r_x = find(x), r_y = find(y);
            if (r_x != r_y) {
                p[r_x] = r_y;
                dis[r_x] = dis[y] - dis[x] + 1;
            }
            else {
                if ((dis[x] - dis[y] - 1) % 3)
                    ++ans; 
            }
        }
    }

    printf("%d", ans);

    fclose(stdin);
    fclose(stdout);
    return 0;
}

并查集维护到祖宗距离是指在并查集的数据结构中,除了记录每个元素的父节点外,还记录了每个元素到其祖宗节点距离。这样可以在查找祖宗节点的同时,更新每个元素到祖宗节点距离。这个功能可以通过路径压缩来实现。路径压缩是指在查找祖宗节点的过程中,将路径上的每个节点的父节点直接指向祖宗节点,从而减少后续查找的时间复杂度。在路径压缩的过程中,还可以同时更新每个节点到祖宗节点距离。这样,在后续的操作中,可以直接通过查找元素的祖宗节点来获取元素到祖宗节点距离,而无需再次进行计算。引用\[1\]中的代码是一个实现路径压缩的示例,其中的d数组就是用来记录每个元素到祖宗节点距离。引用\[2\]和引用\[3\]中也提到了路径压缩的概念和实现方式。 #### 引用[.reference_title] - *1* [AcWing 238. 银河英雄传说--(维护size和到祖宗节点距离)带权并查集](https://blog.youkuaiyun.com/qq_45748404/article/details/117967117)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [算法基础课—数据结构(六) 并查集](https://blog.youkuaiyun.com/Yttttttttttttttt/article/details/117067020)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值