-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedundantConnection.cpp
64 lines (54 loc) · 1.43 KB
/
RedundantConnection.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* Coding challenge #684 from LeetCode */
class DisjointSet {
public:
DisjointSet(int size) {
parent.resize(size);
for (int i = 1; i < size; ++i) {
parent[i] = i;
}
rank.resize(size);
}
int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
bool merge(int x, int y) {
int x_rank = find(x);
int y_rank = find(y);
if (x_rank == y_rank) {
return false;
}
else if (rank[x_rank] < rank[ y_rank]) {
parent[x_rank] = y_rank;
}
else if (rank[x_rank] > rank[ y_rank]) {
parent[ y_rank] = x_rank;
}
else {
parent[ y_rank] = x_rank;
rank[x_rank]++;
}
return true;
}
private:
vector<int> parent;
vector<int> rank;
};
class Solution {
public:
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
set<int> vertices;
vector<int> result;
for (int i = 0; i < edges.size(); i++) {
vertices.insert(edges[i][0]);
vertices.insert(edges[i][1]);
}
DisjointSet disjoint_set(vertices.size()+1);
for (auto edge : edges) {
if (!disjoint_set.merge(edge[0], edge[1])) return edge;
}
return result;
}
};