算法学习笔记之并查集模板

并查集

并查集的实现是通过合并和查找实现的

  1. 第一步初始化,定义数组 int s[]是以节点i为元素的并查集,在开始的时候每个点属于独立的集,s[i]=i;

    void init_set()//初始化数组
    {
    	for (int i = 1; i < maxn; i++)
    		s[i] = i;
    }
    
  2. 第二步合并,例如把节点1合并到节点2,也就是把节点1的集改成节点2的集

    void union_set(int x, int y)//合并
    {
    	x = find_set(x);//查找x对应的节点
    	y = find_set(y);//查找y对应的节点
    	if (x != y) s[x] = s[y];
    }
    
  3. 第三步查找,查找元素的集是一个递归的过程,知道元素的值和他的集相同就等于找到根节点的集

    int find_set(int x)//查找
    {
    	return x == s[x] ? x : find_set(s[x]);
    }
    

    直接来看例题How Many Tables HDU - 1213
    Today is Ignatius’ birthday. He invites a lot of friends. Now it’s dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.

    One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.

    For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
    Input
    The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
    Output
    For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
    Sample Input
    2
    5 3
    1 2
    2 3
    4 5

    5 1
    2 5
    Sample Output
    2
    4
    有n个人吃饭,有些人相互认识,认识的人想做在一起,不想跟陌生人坐在一起,例如A认识B,B认识C,那么ABC会坐在一张桌子上,现在给出认识的人,问需要多少张桌子
    思路是一张桌子是一个集,合并朋友关系,然后统计集的数量就是桌子的数量

    	#include<iostream>
    	#include<algorithm>
    	using namespace std;
    	
    	const int maxn = 1005;
    	int s[maxn];
    	void init_set()//初始化数组
    	{
    		for (int i = 1; i < maxn; i++)
    			s[i] = i;
    	}
    	int find_set(int x)//查找
    	{
    		return x == s[x] ? x : find_set(s[x]);
    	}
    	
    	void union_set(int x, int y)//合并
    	{
    		x = find_set(x);
    		y = find_set(y);
    		if (x != y) s[x] = s[y];
    	}
    	int main()
    	{
    		int t;
    		cin >> t;
    		while (t--) {
    			int n, m, x, y;
    			cin >> n >> m;
    			init_set();
    			for (int i = 1; i <= m; i++) {
    				cin >> x >> y;
    				union_set(x, y);//合并x,y
    			}
    			int ans = 0;
    			for (int i = 1; i <= n; i++) {
    				if (s[i] == i)//根节点的数量
    					ans++;
    			}
    			cout << ans << endl;
    		}
    		return 0;
    	
    

    复杂度是O(n),性能较差下面是并查集的优化

    合并优化

    在合并x,y是先搜到他们的根节点,然后再合并这两个节点,即把一个根节点的集改成另一个根节点。这两个根节点的高度不同,如果把高度较小的集合合并到较大的集上,能减小树的高度。用hight[]定义i的高度

    void init(int n)
    {
    	for (int i = 1; i <= n; i++) {
    		s[i] = i;
    		height[i] = 0;//树的高度
    	}
    }
    void Union(int x, int y)
    {
    	x = find(x);
    	y = find(y);
    	if (height[x] == height[y]) {
    		height[x] = height[x] + 1;合并树的高度加1
    		s[y] = x;
    	}
    	else {                                //把矮树并到高树上,高树的高度不变
    		if (height[x] < height[y])
    			s[x] = y;
    		else
    			s[y] = x;
    	}
    }
    

    查询优化—路径压缩

    上面的程序中查询i所属的集需要搜索路径一路上去找到根节点,返回的结果是根节点。这条路可能很长。如果再返回的时候顺便把i的集改成根节点,那么下次再搜的时候就直接得到根节点

    int find(int x)
    {
    	if (x != s[x])s[x] = find(s[x]);//路径压缩
    	return s[x];
    }
    

    这一步使得这个集里面所有的元素的值都为根节点。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值