並查集操作

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>
#include <sstream>
#include <ostream>
#include <stack>
#include <queue>
#include <cmath>
#include <stdlib.h>
#include <ctype.h>
#include <map>
#define inf 1e9+7
using namespace std;
int fa[1005];
int height[1005];
void init_union(int n)
{
    for(int i=0;i<n;i++)
    {
        fa[i]=i;
        height[i]=0;
    }
}
int find(int x)
{
    if(fa[x]==x) return x;
    else return fa[x]=find(fa[x]);
}
void unite(int x,int y)
{
    x=find(x);
    y=find(y);
    if(x==y) return ;
    if(height[x]<height[y])
        fa[x]=y;
    else
    {
        fa[y]=x;
        if(height[y]==height[x]) ++height[x];
    }
}
bool same(int x,int y)
{
    return find(x)==find(y);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章