forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths1.cpp
More file actions
37 lines (37 loc) · 995 Bytes
/
s1.cpp
File metadata and controls
37 lines (37 loc) · 995 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// OJ: https://leetcode.com/problems/satisfiability-of-equality-equations/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(1)
class UnionFind {
unordered_map<char, char> id;
unordered_map<char, int> rank;
public:
void connect(char a, char b) {
int x = find(a), y = find(b);
if (x == y) return;
if (rank[x] <= rank[y]) {
id[x] = y;
if (rank[x] == rank[y]) rank[y]++;
} else id[y] = x;
}
char find(char a) {
if (!id.count(a)) {
rank[a] = 1;
id[a] = a;
}
return id[a] == a ? a : (id[a] = find(id[a]));
}
};
class Solution {
public:
bool equationsPossible(vector<string>& equations) {
UnionFind uf;
for (auto e : equations) {
if (e[1] == '=') uf.connect(e[0], e[3]);
}
for (auto e : equations) {
if (e[1] == '!' && uf.find(e[0]) == uf.find(e[3])) return false;
}
return true;
}
};