Given an array equations of strings that represent relationships between variables, each string equations[i] has length 4 and takes one of two different forms: "a==b" or "a!=b".  Here, a and b are lowercase letters (not necessarily different) that represent one-letter variable names.

Return true if and only if it is possible to assign integers to variable names so as to satisfy all the given equations.

Example 1:

Input: ["a==b","b!=a"]
Output: false
Explanation: If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second. There is no way to assign the variables to satisfy both equations.

Example 2:

Input: ["b==a","a==b"]
Output: true
Explanation: We could assign a = 1 and b = 1 to satisfy both equations.

Example 3:

Input: ["a==b","b==c","a==c"]
Output: true

Example 4:

Input: ["a==b","b!=c","c==a"]
Output: false

Example 5:

Input: ["c==c","b==d","x!=z"]
Output: true

Note:

  1. 1 <= equations.length <= 500
  2. equations[i].length == 4
  3. equations[i][0] and equations[i][3] are lowercase letters
  4. equations[i][1] is either '=' or '!'
  5. equations[i][2] is '='
 
 
 
class Solution {
private:
int f[]; void init(){
for (int i=;i<;i++)
f[i] = i;
} int get_f(int x) {
if (int(f[x]) == int(x)) {
return x;
}
return f[x] = get_f(f[x]); // wrong
} void merge(int a, int b){
a = get_f(a); // wrong
b = get_f(b);
f[a] = b;
} bool is_same_set(int a, int b){
if (get_f(a) == get_f(b))
return true;
return false;
} public:
bool equationsPossible(vector<string>& equations) {
init();
for (string s : equations){ // first: ==
cout<<s<<endl;
int a = s[] - 'a'; // wrong
int b = s[] - 'a';
if (s[] == '=')
merge(a, b);
} for (string s : equations){ // second: !=
cout<<s<<endl;
int a = s[] - 'a'; // wrong
int b = s[] - 'a';
if (s[] == '!')
if (is_same_set(a, b))
return false;
}
return true;
}
};

【medium】990. Satisfiability of Equality Equations 并查集的更多相关文章

  1. LC 990. Satisfiability of Equality Equations

    Given an array equations of strings that represent relationships between variables, each string equa ...

  2. 【leetcode】990. Satisfiability of Equality Equations

    题目如下: Given an array equations of strings that represent relationships between variables, each strin ...

  3. 【LeetCode】990. Satisfiability of Equality Equations 解题报告(C++ & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 并查集 日期 题目地址:https://le ...

  4. LeetCode 990. Satisfiability of Equality Equations

    原题链接在这里:https://leetcode.com/problems/satisfiability-of-equality-equations/ 题目: Given an array equat ...

  5. Satisfiability of Equality Equations - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Satisfiability of Equality Equations - LeetCode 注意点 必须要初始化pre 解法 解法一:典型的并查集算法 ...

  6. [Swift]LeetCode990. 等式方程的可满足性 | Satisfiability of Equality Equations

    Given an array equations of strings that represent relationships between variables, each string equa ...

  7. [leetcode] 并查集(Ⅱ)

    最长连续序列 题目[128]:链接. 解题思路 节点本身的值作为节点的标号,两节点相邻,即允许合并(x, y)的条件为x == y+1 . 因为数组中可能会出现值为 -1 的节点,因此不能把 root ...

  8. LeetCode:并查集

    并查集 这部分主要是学习了 labuladong 公众号中对于并查集的讲解,文章链接如下: Union-Find 并查集算法详解 Union-Find 算法怎么应用? 概述 并查集用于解决图论中「动态 ...

  9. 【并查集专题】【HDU】

    PS:做到第四题才发现 2,3题的路径压缩等于没写 How Many Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65 ...

随机推荐

  1. 注意:QQ影音视频压缩时长丢失

    客户宣传片发来,高清的,比较大,500多M,需要转成小一点的,放在客户网站上,于是用QQ影音转码压缩下,变成低质量的.如下 一切都很顺利,提示进度100%! 这一切都是电脑自动的,又是提示成功的,千想 ...

  2. 用redis实现分布式锁,秒杀案例(转)

    分布式锁的简单实现代码: 需要的jar包: jedis-2.9.0.jar. commons-pool2-2.4.2.jar import java.util.List; import java.ut ...

  3. JS学习笔记:(三)JS执行机制

    首先我们先明确一点:JavaScript是一门单线程语言.单线程也就是说同一时间只能执行一个任务,所有的任务都必须排队顺序执行.那么如果一个任务耗时很长,阻塞了其它任务的执行,就会给用户造成不友好的体 ...

  4. 新建promise

    callback(){ const promise =new Promise((resolve,rejest)=>{ console.log('2') 代码 resolve('true') }) ...

  5. ImageMagick 安装 window10与错误总结

    一般默认安装最新的版本(目前是7.0.8),但是通过测试发现这个版本会报错(ImportError: MagickWand shared library not found.)所以建议安装 im6版本 ...

  6. ADO.Net笔记整理(一)

    几次装机,Notes已烟消云散,近日因为Node.js死活搞不定,无奈装机,备份好的东东,没想到磁盘扇区出现异常,可能是PE启动盘的病毒,只好将磁盘全部重新分区,恢复数据也懒得恢复了,日积月累关乎将来 ...

  7. PHP 面向切面编程

    面向切面编程介绍: 介绍: AOP(Aspect-Oriented Programming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和 ...

  8. es定期删除数据

    es定期删除数据 1.定期删除索引 使用sentinl报警后,会产生大量如下索引,虽然不占空间,但时间久了也不好,故写个脚本定期删除 脚本如下: 1 #!/bin/bash 2 #只保留5天内的日志索 ...

  9. bzoj4490 随机数生成器Ⅱ加强版

    题目链接 题意 给出参数\(C_1,C_2,P\)按如下方式生成一个长度为\(n \times m\)的序列\(x\): \(x_0 = C_1,x_1=C2\) \(x_i=(x_{i-1}+x_{ ...

  10. Java反射-修改字段值, 反射修改static final修饰的字段

    反射修改字段 咱们从最简单的例子到难, 一步一步深入. 使用反射修改一个private修饰符的变量name 咱们回到主题, 先用反射来实现一个最基础的功能吧. 其中待获取的name如下: public ...