并查集C++模板
发布时间
阅读量:
阅读量
并查集属于一种以树形结构为基础的数据组织方式,主要用于解决多个互不相交集合的合并与检索问题。若提供一个方向图,借助并查集算法能够高效地判定任意两个节点是否归属于同一集合。
#include<iostream>
#include<stdio.h>
#include<string.h>
#define MAXN 1000
using namespace std;
int root[MAXN]; //the root of a node
int layer[MAXN];
int n, m; //the number of node and edge
void init(){
for (int i = 0; i < n; i++){
root[i] = i;
layer[i] = 0;
}
}
//find the finaly root and updata the journey
int find_root(int a){
if (root[a] == a ) return a;
return root[a] = find_root(root[a]);
}
//connect tow nod
全部评论 (0)
还没有任何评论哟~
