码迷,mamicode.com
首页 > 其他好文 > 详细

并查集

时间:2015-04-26 16:49:11      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:并查集

高级版

码:

#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int father[N];//父亲
int n,m,a,b;
int rank[N];//树的高度
int find(int x)//查询树的根
{
    if(x==father[x]) return x;
    else return father[x]=find(father[x]);
}
void Merge(int a,int b)//合并a和b所属的集合
{
    int pa=find(a);
    int pb=find(b);
    if(pa==pb) return;
    if(rank[pa]<rank[pb])  father[pa]=pb;
    else {
        father[pb]=pa;
        if(rank[pa]==rank[pb]) rank[pa]++;
    }
}
bool same(int a,int b)//判断a和b是否属于同一集合
{
    return find(a)==find(b);
}


并查集

标签:并查集

原文地址:http://blog.csdn.net/u013050857/article/details/45288569

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!