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. Appuim的安装步骤

    1.下载Appium Desktop并安装 下载地址:https://github.com/appium/appium-desktop/releases 我下载的版本为:appium-desktop- ...

  2. Map的clear与new Map的区别

    对于clear与new Map的区别.我们首先来看一个例子,本例子是我在实际开发中遇到的,需求就是讲map放入到list中,说白了就是list转map,有两种实现方式,分别是: // 方案一 Map& ...

  3. java订单金额分级计算

    package ord; import java.util.ArrayList; public class order { public String orderid; public user use ...

  4. mysql-笔记-类型转化

    1 concat() 隐式转化为字符串 2 cast( 1 as char) 显示转化 3 比较中的隐式转化 null 与任何值比例都是null : 除了使用 null-safe <=> ...

  5. Cordova Upload Images using File Transfer Plugin and .Net core WebAPI

    In this article, I am going to explain ,"How to Upload Images to the server using Cordova File ...

  6. ORACLE数据闪回

    ALTER TABLE SPM_CON_PAYMENT_RECEIPT ENABLE ROW MOVEMENT;   -- 表名 FLASHBACK TABLE SPM_CON_PAYMENT_REC ...

  7. P4137 Rmq Problem / mex (莫队)

    题目 P4137 Rmq Problem / mex 解析 莫队算法维护mex, 往里添加数的时候,若添加的数等于\(mex\),\(mex\)就不能等于这个值了,就从这个数开始枚举找\(mex\): ...

  8. HTML&CSS_基础02

    一.实体 # 1. 一些符号如 “<”. “>”. “ ”. ”©“等,不能使用其本身,需要借助实体(即转义字符),格式通常为:& (字符对应实体); 如 &lt . &a ...

  9. [BJOI2019]奥术神杖

    https://www.luogu.org/problemnew/show/P5319 题解 首先观察我们要求的答案的形式: \[ \biggl(\prod V_i \biggr)^x\ \ \ x= ...

  10. 微信小程序地图控件篇 ---自定义图标被地图覆盖的问题

    今天在做微信小程序的时候遇到这个这样的问题  需要在地图上加个一个自定义的图标控件 类似这样的 刚开始的时候怎图片一直会被地图组件覆盖  ,要怎么解决这个问题  我去翻了下小程序的文档 有个cover ...