#include <iostream>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#define MAX 10000
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int par[MAX];
int rank[MAX];
void unit(int n)
{
for(int i=0;i<n;i++)
{
par[i]=i;
rank[i]=0;
}
}
int find(int x)
{
if(par[x]==x)
{
return x;
}
else
{
return par[x]=find(par[x]);//调整高度
}
}
void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x==y)
{
return;
}
if(rank[x]<rank[y])//x!=y将x放到y子树
{
par[x]=y;
}
else
{
par[y]=x;//将y放x子树
if(rank[x]==rank[y])
{
rank[x]++;
}
}
}
bool same(int x,int y)
{
return find(x)==find(y);
}
int main(void)
{
return 0;
}
并查集的实现
最新推荐文章于 2024-03-19 11:11:11 发布