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. Python selenium —— 一定要会用selenium的等待,三种等待方式解读

    发现太多人不会用等待了,博主今天实在是忍不住要给大家讲讲等待的必要性. 很多人在群里问,这个下拉框定位不到.那个弹出框定位不到…各种定位不到,其实大多数情况下就是两种问题:1 有frame,2 没有加 ...

  2. feilong's blog | 目录

    每次把新博客的链接分享到技术群里,我常常会附带一句:蚂蚁搬家.事实上也确实如此,坚持1篇1篇的把自己做过.思考过.阅读过.使用过的技术和教育相关的知识.方法.随笔.索引记录下来,并持续去改进它们,希望 ...

  3. H5软键盘兼容方案

    前言 最近一段时间在做 H5 聊天项目,踩过其中一大坑:输入框获取焦点,软键盘弹起,要求输入框吸附(或顶)在输入法框上.需求很明确,看似很简单,其实不然.从实验过一些机型上看,发现主要存在以下问题: ...

  4. PHP秒杀系统 高并发高性能的极致挑战(完整版)

    需要的联系我,QQ:1844912514 4-1 商品页面开发--服务端代码 8-8 提高数据处理速度-代码改造(一)

  5. 米卡 Mica Logo 存放处

  6. git基本概念

    工作区:电脑上存放源代码的文件夹 版本库:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库.       版本库包含stage(暂存区),master分支,以及指向master分支的指 ...

  7. [oracle]解决ora-01034 oracle not available

    一般都是数据库连接中断了,按照链接重连即可. https://jingyan.baidu.com/article/5552ef47c73eef518ffbc908.html

  8. 修改CentOS6.5默认主机名(root下操作)

    使用CentOS6.5官方镜像安装完毕之后,默认的主机名为localhost,不便管理,我们需要根据实际情况修改. 此处我准备讲默认的主机名 localhost 改为 comex01-ct65 第一步 ...

  9. [PKUWC2019]Day1 T2 你和虚树的故事

    选择k个颜色,使得颜色的虚树有交的方案数 肯定要考虑连通块的贡献. 法一 https://www.cnblogs.com/xzz_233/p/10292983.html 枚举连通块还是不可行的. 枚举 ...

  10. pwn-格式化字符串漏洞

    原理:因为没有正确使用printf()函数 正确使用 : printf('%s',str) 不正规使用:printf(str) 控制字符串str可以爆出stack内内容从而实现任意地址读或者任意地址写 ...