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:

  1. Input: ["a==b","b!=a"]
  2. Output: false
  3. 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:

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

Example 3:

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

Example 4:

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

Example 5:

  1. Input: ["c==c","b==d","x!=z"]
  2. 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 '='
 
 
 
  1. class Solution {
  2. private:
  3. int f[];
  4.  
  5. void init(){
  6. for (int i=;i<;i++)
  7. f[i] = i;
  8. }
  9.  
  10. int get_f(int x) {
  11. if (int(f[x]) == int(x)) {
  12. return x;
  13. }
  14. return f[x] = get_f(f[x]); // wrong
  15. }
  16.  
  17. void merge(int a, int b){
  18. a = get_f(a); // wrong
  19. b = get_f(b);
  20. f[a] = b;
  21. }
  22.  
  23. bool is_same_set(int a, int b){
  24. if (get_f(a) == get_f(b))
  25. return true;
  26. return false;
  27. }
  28.  
  29. public:
  30. bool equationsPossible(vector<string>& equations) {
  31. init();
  32. for (string s : equations){ // first: ==
  33. cout<<s<<endl;
  34. int a = s[] - 'a'; // wrong
  35. int b = s[] - 'a';
  36. if (s[] == '=')
  37. merge(a, b);
  38. }
  39.  
  40. for (string s : equations){ // second: !=
  41. cout<<s<<endl;
  42. int a = s[] - 'a'; // wrong
  43. int b = s[] - 'a';
  44. if (s[] == '!')
  45. if (is_same_set(a, b))
  46. return false;
  47. }
  48. return true;
  49. }
  50. };

【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. Spring Boot – Jetty配置

    前言 默认情况下,Spring Boot会使用内置的tomcat容器去运行应用程序,但偶尔我们也会考虑使用Jetty去替代Tomcat:对于Tomcat和Jetty,Spring Boot分别提供了对 ...

  2. c++ _pFirstBlock == pHead

    今天写程序时碰到了这个异常,导致调试的程序卡死.在网上找了很久答案,都没解决.大致判定是对象被多次析构,但又确认程序逻辑没有问题. 后来参考了 http://www.cnblogs.com/qinta ...

  3. Java的selenium代码随笔(6)

    //获取元素列表public List<WebElement> ListElements(WebElement webElement, By parentBy, By childrenBy ...

  4. 转:eclipse 设置Java快捷键补全

    1.打开Eclipse,点击" Window - Preferences"; 2. 在目录树上选择"Java——Editor——Content Assist", ...

  5. telnet-server、telnet

    1.查询yum仓库中的安装包 [root@localhost /]# yum list |grep telnettelnet.x86_64                              1 ...

  6. 【学亮编程手记】Spring Cloud三大组件Eureka/Feign/Histrix的原理及使用

  7. vue.js实战——.native修饰符

    https://blog.csdn.net/qq_29468573/article/details/80771625 除了用v-on在组件上监听自定义事件外,也可以监听DOM事件,这时可以用.nati ...

  8. DAY19、日常模块

    一.hashlib模块:加密1.基本使用:import hashlibcipher = hashlib.md5('需要加密的数据(二进制形式)'.encode('utf-8'))print(ciphe ...

  9. servlet(6) 链接数据库

    一.servlet链接mysql步骤: 1.注册驱动器:Class.forName("com.mysql.jdbc.Driver"); 加载类并执行下面的静态语句块,将Driver ...

  10. Python——代码汇总

    1.三级菜单 2.Windows启动服务 3.常用的Python实现 4.字典的基本操作