目录
一.并查集的相关概念
二.并查集的相关操作及其实现
一.并查集的相关概念
并查集是一种树型的数据结构,用于处理一些不相交集合的合并及查询问题。
并查集的思想是用一个数组表示了整片森林(parent),树的根节点唯一标识了一个集合,我们只要找到了某个元素的的树根,就能确定它在哪个集合里。
二.并查集的相关操作及其实现
查集的思想是通过标记确定该顶点所在的组。
所以对于一个n个点,m条边的图,我们需要新建一个长度为n的数组f(可以理解为father),f[n]代表点n的团伙“代表人”,当两个点所在团伙“代表人”相同,则这两个点所在团伙相同。
而在最开始,每个顶点间都是互相不连通的,所以每个顶点单独属于一个团伙,每个顶点理所应当成为自己团伙的“代表人”,所以我们把f[n]的初始值赋为n。
和并团伙:
比如说我们要合并 1 和3让他们做一个团伙,既然变成团伙了那么就一定有代言人,那么肯定是实力强的一方做代言人,现在1 和3这两个的力量一样大这时候了就假设我们让3做代言人:
刚刚我们合并了3和1,现在我们需要合并3和2。如果按照f[a] = b这样合并,那么,f[3]就被赋值为了2。这样,f[3]原本的值1就被覆盖了,也就是说,1和3的团伙就被硬生生地“拆散”了。所以我们需要这样定义f[a的团伙代表人] = (b的团伙代表人)。
判断两个团队的代表人是否相同:
我们同通过查找他们的代表人是否相等即可。下面请看泛型的代码:
#include
#include
#include
#include
#include
using namespace std;
template
struct Node {
Node(V v)
:val(v)
{}
V val;
};
template
class UnionFind {
public:
UnionFind(int N) {
for(int value=1;value<=N;value++){
Node* node = new Node(value);//创建一个节点
nodes.insert(make_pair(value, node));//插入
parent.insert(make_pair(node, node));//一开始自己指向自己
sizeMap.insert(make_pair(node, 1));//一开始集合的个数为1
}
}
Node* findFather(Node* cur) {//寻找其父亲节点
stack*>path;
while (cur != parent[cur]) {//知道不能够再往上了
path.push(cur);//记录这条路径上的节点
cur = parent[cur];//一直往上走
}
while (!path.empty()) {//路径压缩做一个优化我们每次都要找代表人都要一直往上找不如找一次找到过程我将其全部设置好
parent.insert(make_pair(path.top(), cur));//将其父亲节点都设置为cur
path.pop();
}
return cur;//
}
bool isSameSet(V a, V b) {
if (!nodes[a] || !nodes[b]) {//看这两个集合是否都存在
return false;
}
return findFather(nodes[a]) == findFather(nodes[b]);//查看他们的父亲节点是否相同
}
bool Union(V a, V b) {
if (!nodes[a] || !nodes[b]) {//首先看其是否都存在
return false;
}
Node* aHead = findFather(nodes[a]);//找到各自对应的父亲节点
Node* bHead = findFather(nodes[b]);
if (aHead != bHead) {
int aSetSize = sizeMap[aHead];
int bSetSize = sizeMap[bHead];
if (aSetSize >= bSetSize){//a集合的大小大于b集合的大小
parent[bHead] = aHead;
sizeMap[aHead] = aSetSize + bSetSize;
sizeMap.erase(bHead);
}
else {
parent[aHead] = bHead;
sizeMap[bHead] = aSetSize + bSetSize;//合并
sizeMap.erase(aHead);//删除
}
}
return true;
}
private:
unordered_map*>nodes;//存储节点
unordered_map*, Node*>parent;//节点的父亲
unordered_map*, int>sizeMap;//每个一集合的个数
};
并查集是一种树型的数据结构,用于处理一些不相交集合的合并及查询问题。
并查集的思想是用一个数组表示了整片森林(parent),树的根节点唯一标识了一个集合,我们只要找到了某个元素的的树根,就能确定它在哪个集合里。
查集的思想是通过标记确定该顶点所在的组。
所以对于一个n个点,m条边的图,我们需要新建一个长度为n的数组f(可以理解为father),f[n]代表点n的团伙“代表人”,当两个点所在团伙“代表人”相同,则这两个点所在团伙相同。
而在最开始,每个顶点间都是互相不连通的,所以每个顶点单独属于一个团伙,每个顶点理所应当成为自己团伙的“代表人”,所以我们把f[n]的初始值赋为n。
和并团伙:
比如说我们要合并 1 和3让他们做一个团伙,既然变成团伙了那么就一定有代言人,那么肯定是实力强的一方做代言人,现在1 和3这两个的力量一样大这时候了就假设我们让3做代言人:
刚刚我们合并了3和1,现在我们需要合并3和2。如果按照f[a] = b这样合并,那么,f[3]就被赋值为了2。这样,f[3]原本的值1就被覆盖了,也就是说,1和3的团伙就被硬生生地“拆散”了。所以我们需要这样定义f[a的团伙代表人] = (b的团伙代表人)。
判断两个团队的代表人是否相同:
我们同通过查找他们的代表人是否相等即可。下面请看泛型的代码:
#include#include #include #include #include
using namespace std; template struct Node { Node(V v) :val(v) {} V val; }; template class UnionFind { public: UnionFind(int N) { for(int value=1;value<=N;value++){ Node * node = new Node (value);//创建一个节点 nodes.insert(make_pair(value, node));//插入 parent.insert(make_pair(node, node));//一开始自己指向自己 sizeMap.insert(make_pair(node, 1));//一开始集合的个数为1 } } Node * findFather(Node * cur) {//寻找其父亲节点 stack *>path; while (cur != parent[cur]) {//知道不能够再往上了 path.push(cur);//记录这条路径上的节点 cur = parent[cur];//一直往上走 } while (!path.empty()) {//路径压缩做一个优化我们每次都要找代表人都要一直往上找不如找一次找到过程我将其全部设置好 parent.insert(make_pair(path.top(), cur));//将其父亲节点都设置为cur path.pop(); } return cur;// } bool isSameSet(V a, V b) { if (!nodes[a] || !nodes[b]) {//看这两个集合是否都存在 return false; } return findFather(nodes[a]) == findFather(nodes[b]);//查看他们的父亲节点是否相同 } bool Union(V a, V b) { if (!nodes[a] || !nodes[b]) {//首先看其是否都存在 return false; } Node * aHead = findFather(nodes[a]);//找到各自对应的父亲节点 Node * bHead = findFather(nodes[b]); if (aHead != bHead) { int aSetSize = sizeMap[aHead]; int bSetSize = sizeMap[bHead]; if (aSetSize >= bSetSize){//a集合的大小大于b集合的大小 parent[bHead] = aHead; sizeMap[aHead] = aSetSize + bSetSize; sizeMap.erase(bHead); } else { parent[aHead] = bHead; sizeMap[bHead] = aSetSize + bSetSize;//合并 sizeMap.erase(aHead);//删除 } } return true; } private: unordered_map *>nodes;//存储节点 unordered_map *, Node *>parent;//节点的父亲 unordered_map *, int>sizeMap;//每个一集合的个数 };
下面我们来看两道OJ题 :
并查集的实现_牛客题霸_牛客网 (nowcoder.com)
题目描述:
由于上面已经说过了在这里就只给出代码:
#include#include #include #include #include
using namespace std; class UnionFind{ public: UnionFind(int n){ parent.resize(n+1);//存储节点对应的父亲节点 size.resize(n+1);//存储集合对应的大小 help.resize(n);//做路径压缩的那个数组 sets=n; for(int i=1;i<=n;i++){ parent[i]=i;//一开始父亲节点 size[i]=1; } } int find(int i){ int hi=0; while(i!=parent[i]){ i=parent[i]; help[hi++]=i; } for(hi--;hi>=0;hi--){ parent[help[hi]]=i;//路径压缩 } return i; } bool isSameSet(int x,int y){ return find(x)==find(y); } void Union(int i,int j){ int aHead=find(i);//查找他的父亲 int bHead=find(j);//查找父亲 if(aHead!=bHead){//如果两个的父亲不相等 if(size[aHead]>=size[bHead]){//a集合元素大一点也就是力量大一点 size[aHead]+=size[bHead]; parent[bHead]=aHead; } else{ size[bHead]+=size[aHead]; parent[aHead]=bHead; } sets--; } } private: vector parent;//存储父亲节点 vector size;//集合的大小个数 vector help;// int sets;//集合的个数 }; int main(){ int N,M; cin>>N>>M; UnionFind t(N); while(M--){ int opt,x,y; scanf("%d%d%d",&opt,&x,&y); if(opt==1){ if(t.isSameSet(x, y)){ printf("Yesn"); } else{ printf("Non"); } } else{ t.Union(x,y); } } return 0; }
剑指 Offer II 116. 省份数量 - 力扣(LeetCode) (leetcode-cn.com)
题目描述:
解题思路:
按照题目的意思来如果isconectin[i][j]相连说明他们相互认识此时我们只需要将其合并即可 最终返回剩余的集合数即可。
对应代码:
class Union { public: Union(int n) { size.resize(n); parent.resize(n); help.resize(n); sets=n; for(int i=0;i=0;hi--)//路径压缩 { parent[help[hi]]=i; } return i;//返回父亲 } int getSets()//获取集合的数量 { return sets; } void unoin(int i,int j)//合并两个集合 { int aHead=findfather(i); int bHead=findfather(j); if(aHead!=bHead) { if(size[aHead]>=size[bHead]) { size[aHead]+=size[bHead]; parent[bHead]=aHead; } else { size[bHead]+=size[aHead]; parent[aHead]=bHead; } --sets; } } private: vector size; vector parent;//存储对应的父亲 vector help;//路径压缩 int sets;//记录集合的数量 }; class Solution { public: int findCircleNum(vector >& isConnected) { int n=isConnected.size(); Union t(n); for(int i=0;i



