Welcome T|

Ke_scholar

园龄:2年1个月粉丝:30关注:10

2023-10-31 19:26阅读: 33评论: 0推荐: 0

并查集模板

#include <bits/stdc++.h>
using namespace std;
struct UFS {
int sz;
vector<int> rank, p;
void link(int x, int y) {
if (x == y)
return;
if (rank[x] > rank[y])
p[y] = x;
else
p[x] = y;
if (rank[x] == rank[y])
rank[y]++;
}
void init(int n) {
sz = n;
rank.resize(n + 1);
p.resize(n + 1);
for (int i = 0; i <= sz; i++) {
p[i] = i;
rank[i] = 0;
}
}
int find(int x) {
return x == p[x] ? x : (p[x] = find(p[x]));
}
void unin(int x, int y) {
link(find(x), find(y));
}
void compress() {
for (int i = 0; i < sz; i++)
find(i);
}
};
//种类并查集 merge(y + n, x),merge(x + n, y)

本文作者:Ke_scholar

本文链接:https://www.cnblogs.com/Kescholar/p/17801082.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   Ke_scholar  阅读(33)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起