-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0990.cpp
More file actions
34 lines (28 loc) · 814 Bytes
/
Copy path0990.cpp
File metadata and controls
34 lines (28 loc) · 814 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
class UnionFind {
vector<int> root, rank;
public:
UnionFind(int n) : root(n), rank(n, 1) { iota(root.begin(), root.end(), 0); }
int find(int x) {
if (x == root[x]) return x;
return root[x] = find(root[x]);
}
void join(int x, int y) {
x = find(x), y = find(y);
if (x != y) {
if (rank[x] > rank[y]) swap(x, y);
root[y] = x;
rank[x] += x == y;
}
}
};
class Solution {
public:
bool equationsPossible(vector<string> &equations) {
UnionFind uf(26);
for (auto &s : equations)
if (s[1] == '=') uf.join(s[0] - 'a', s[3] - 'a');
for (auto &s : equations)
if (s[1] == '!' && uf.find(s[0] - 'a') == uf.find(s[3] - 'a')) return false;
return true;
}
};