-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path1361.cpp
More file actions
47 lines (39 loc) · 1.17 KB
/
Copy path1361.cpp
File metadata and controls
47 lines (39 loc) · 1.17 KB
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
38
39
40
41
42
43
44
45
46
47
class UnionFind {
int n;
vector<int> root, rank;
public:
UnionFind(int n) : n(n), root(n), rank(n, 1) { iota(root.begin(), root.end(), 0); }
int find(int x) {
while (x != root[x])
x = root[x] = root[root[x]];
return 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[x] = y;
rank[y] += rank[x];
n--;
}
}
int count() { return n; }
};
class Solution {
bool process(UnionFind &uf, vector<int> &parent, int start, int end) {
if (end == -1) return true;
if (parent[end] != -1) return false;
if (uf.find(start) == uf.find(end)) return false;
uf.join(start, end);
parent[end] = start;
return true;
}
public:
bool validateBinaryTreeNodes(int n, vector<int> &leftChild, vector<int> &rightChild) {
UnionFind uf(n);
vector<int> parent(n, -1);
for (int i = 0; i < n; i++)
if (!process(uf, parent, i, leftChild[i]) || !process(uf, parent, i, rightChild[i])) return false;
return uf.count() == 1;
}
};