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. 解决android studio引用远程仓库下载慢(JCenter下载慢)

    使用开源中国的maven库 阿里云的(速度飞快):http://maven.aliyun.com/nexus/content/groups/public/ 替换项目根目录下build.gradle中的 ...

  2. web 应用常见安全漏洞

    1. SQL 注入 SQL 注入就是通过给 web 应用接口传入一些特殊字符,达到欺骗服务器执行恶意的 SQL 命令. SQL 注入漏洞属于后端的范畴,但前端也可做体验上的优化. 原因 当使用外部不可 ...

  3. HTML 5 & checkbox & switch components

    HTML 5 & checkbox & switch components <!DOCTYPE html> <html lang="zh-Hans" ...

  4. [模板] tarjan/联通分量/dfs树

    //to update 边的分类 有向图边分为四类: 树边, 前向边, 返祖边(后向边), 横叉边. 上图: 判定 有向图 对图进行dfs, 不考虑已经遍历过的点, 得到dfs序 \(dfn_i\). ...

  5. 树莓派3B+(二)

    一.安装SSH工具 这里用的是putty,下载下来是一个exe文件,点开就能用. 下载地址:https://www.chiark.greenend.org.uk/~sgtatham/putty/lat ...

  6. JSP/JSF从web.xml中取出context-param的配置信息

    JSP/JSF从web.xml中取出context-param的配置信息. 应用场景:我们配置了项目的版本信息,想让他显示在页面上,如: <context-param><!-- ## ...

  7. 一文入门HTML5

    1.HTML5 上节回顾:一文读懂ES6(附PY3对比) | 一文入门NodeJS 演示demo:https://github.com/lotapp/BaseCode/tree/master/java ...

  8. win 10 dpi:150% 与 win 7 dpi:150% 的不同之处

    由于 win 7 和 win 10 的 dpi 处理方式不同,导致我们写的客户端程序在 win 7 上运行正常,在 win 10(dpi:150%)上运行不正常了. 具体的描述,可参考:解决win10 ...

  9. [BZOJ4318] OSU!

    比较简单,每个键分两种情况计算期望. 然而要注意的是,期望是线性运算,期望的平方不是平方的期望 . #include <cmath> #include <queue> #inc ...

  10. Apache Shiro Java反序列化漏洞分析

    1. 前言 最近工作上刚好碰到了这个漏洞,当时的漏洞环境是: shiro-core 1.2.4 commons-beanutils 1.9.1 最终利用ysoserial的CommonsBeanuti ...