990. 等式方程的可满足性 leetcode

给定一个由表示变量之间关系的字符串方程组成的数组,每个字符串方程 equations[i] 的长度为 4,并采用两种不同的形式之一:"a==b""a!=b"。在这里,a 和 b 是小写字母(不一定不同),表示单字母变量名。

只有当可以将整数分配给变量名,以便满足所有给定的方程时才返回 true,否则返回 false。 
示例 1:

输入:["a==b","b!=a"]
输出:false
解释:如果我们指定,a = 1 且 b = 1,那么可以满足第一个方程,但无法满足第二个方程。没有办法分配变量同时满足这两个方程。
示例 2:

输出:["b==a","a==b"]
输入:true
解释:我们可以指定 a = 1 且 b = 1 以满足满足这两个方程。
示例 3:

输入:["a==b","b==c","a==c"]
输出:true
示例 4:

输入:["a==b","b!=c","c==a"]
输出:false
示例 5:

输入:["c==c","b==d","x!=z"]
输出:true

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/satisfiability-of-equality-equations
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

根据题目我们可以很清楚的知道我们需要使用 图 的算法,而这个题目我们只需要保证他们在同一集合内就可以,所以使用并查集更合适,当然如果直接构造一张图也不是不可以,当时要使用搜索算法,从时间上来说并不合适;

class Solution {
    public:
    int uset[500];
    int rank[500];
 
    void makeSet(int size) {
        for(int i = 0;i < size;i++)  uset[i] = i;
        for(int i = 0;i < size;i++)  rank[i] = 0;
    }
    int find(int x) {
        if (x != uset[x]) uset[x] = find(uset[x]);
        return uset[x];
    }
    void unionSet(int x, int y) {
        if ((x = find(x)) == (y = find(y))) return;
        if (rank[x] > rank[y]) uset[y] = x;
        else {
            uset[x] = y;
            if (rank[x] == rank[y]) rank[y]++;
        }
    }
    bool equationsPossible(vector<string>& equations) {
        makeSet(30);
        for(auto it = equations.begin(); it != equations.end(); ++it){
            string str = (*it);
             if(str[1] == '=')
                 unionSet(str[0]-'a', str[3] - 'a');
        }
        bool re = true;
        for(auto it = equations.begin(); it != equations.end(); ++it){
            string str = (*it);
            if(str[1] == '='){
                re = re && (find(str[0] - 'a') == find(str[3] - 'a'));
            }else{
                re = re && (find(str[0] - 'a') != find(str[3] - 'a'));
            }
        }
        return re;
    }
};

最初级的并查集实际上

 	int uset[500];
 	 
    void makeSet(int size) {
        for(int i = 0;i < size;i++)  uset[i] = i;
    }
    int find(int x) {
        if (x == uset[x])
        	return x;
        return find(uset[x]);
    }
    void unionSet(int x, int y) {
    	int s1 = find(x);
    	int s2 = find(y);
    	if(s1 == s2)
    		return;
    	else
    		uset[x] = y;
    }

但实际上这个算法的缺陷很明显, 随着集合的不断增大,树不断变深,搜索所花费的时间将越来越大
所以优化方式有两种

	void unionSet(int x, int y) {
        if ((x = find(x)) == (y = find(y))) return;
        if (rank[x] > rank[y]) uset[y] = x;
        else {
            uset[x] = y;
            if (rank[x] == rank[y]) rank[y]++;
        }
    }

按轶插入,我们总是将轶较小的树插在轶较大的树下面,这样可以防止树不断变深,增加搜索耗时;
单元素的树的秩定义为0,当两棵秩同为r的树联合时,它们的秩r+1(轶和深度不同,轶不等于深度,wiki是这么写的。。。我也不知道为什么)

	int find(int x) {
        if (x != uset[x]) uset[x] = find(uset[x]);
        return uset[x];
    }

路径压缩,将搜索路径上的每个节点,直接指向根节点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值