您的位置:首页 > 理论基础 > 数据结构算法

模板-并查集

2016-09-30 21:42 288 查看
#include <iostream>
using namespace std;

int father[100];

//初始化
void init()
{
for(int i = 0; i < 100; ++ i)
{
father[i] = i;
}
}

//查找根节点,按路径优化
int find(int node)
{
if(father[node] != node)
{
father[node] = find(father[node]);
}
return father[node];
}

//合并
void unit(int x, int y)
{
int x0 = find(x);
int y0 = find(y);
father[y0] = x0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息