并查集111111

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;

int s[N];
int height[N];//高度 

void init_set()//初始化 
{
    
    
	for (int i = 1; i <= N; i ++ ) 
	{
    
    
		s[i] = i;
		height[i] = 0;
	}
}

int find(int x)//找祖宗节点 
{
    
    
	if (x != s[x]) s[x] = find (s[x]);
	return s[x];
}

void hebing(int x, int y)//合并 
{
    
    
	x = find(x);
	y = find(y);
	if (height[x] == height[y])
	{
    
    
		height[x] = height[x] + 1;
		s[y] = x;
	}
	else
	{
    
    
		if (height[x] < height[y]) s[x] = y;
		else s[y] = x;
	}
}

int main()
{
    
    
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34682765/article/details/121431748
今日推荐