# 990. Satisfiability of Equality Equations 等式方程的可满足性

@TOC

## # 题目描述

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. equationsiopen in new window is either '=' or '!'
5. equations[i][2] is '='

## # 解题方法

### # DFS

python代码如下：

``````class Solution(object):
def equationsPossible(self, equations):
"""
:type equations: List[str]
:rtype: bool
"""
self.m = collections.defaultdict(list)
for eq in equations:
if eq[1] == '=':
self.m[eq[0]].append(eq[3])
self.m[eq[3]].append(eq[0])
for eq in equations:
if eq[1] == '!':
if self.find(set(), eq[0], eq[3]) or self.find(set(), eq[3], eq[0]):
return False
return True

def find(self, visited, begin, end):
if begin in visited:
return False
if begin == end:
return True
for n in self.m[begin]:
if self.find(visited, n, end):
return True
return False
``````

### # 并查集

python代码如下：

``````class Solution(object):
def equationsPossible(self, equations):
"""
:type equations: List[str]
:rtype: bool
"""
dsu = DSU()
for eq in equations:
if eq[1] == '=':
dsu.u(ord(eq[0]) - ord('a'), ord(eq[3]) - ord('a'))
for eq in equations:
if eq[1] == '!':
if dsu.f(ord(eq[0]) - ord('a')) == dsu.f(ord(eq[3]) - ord('a')):
return False
return True

class DSU(object):
def __init__(self):
self.m = range(26)

def f(self, x):
if self.m[x] != x:
self.m[x] = self.f(self.m[x])
return self.m[x]

def u(self, x, y):
px = self.f(x)
py = self.f(y)
self.m[px] = py
``````

C++代码如下：

``````class Solution {
public:
bool equationsPossible(vector<string>& equations) {
init();
for (string& s : equations) {
if (s[1] == '=') {
u(s[0] - 'a', s[3] - 'a');
}
}
for (string& s : equations) {
if (s[1] == '!') {
if (f(s[0] - 'a') == f(s[3] - 'a'))
return false;
}
}
return true;
}
private:
int _fa[26];

void init() {
for (int i = 0; i < 26; ++i) {
_fa[i] = i;
}
}
int f(int x) {
if (_fa[x] == x) return x;
return _fa[x] = f(_fa[x]);
}
void u(int a, int b) {
int pa = f(a);
int pb = f(b);
_fa[pa] = pb;
}
};
``````

## # 日期

2019 年 2 月 21 日 —— 一放假就再难抓紧了