-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path684.cpp
35 lines (34 loc) · 886 Bytes
/
684.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
class Solution {
public:
struct UnionSet {
int *fa;
UnionSet(int n) {
fa = new int[n + 1];
for (int i = 0; i <= n; i++) {
fa[i] = i;
}
return ;
}
int get(int x) {
return (fa[x] = (x - fa[x] ? get(fa[x]) : x));
}
int merge(int a, int b) {
if (get(a) == get(b)) return 0;
fa[get(a)] = get(b);
return 1;
}
};
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
UnionSet u(edges.size());
vector<int> ret;
for (int i = 0; i < edges.size(); i++) {
int a = edges[i][0];
int b = edges[i][1];
if (u.merge(a, b)) continue;
ret.push_back(a);
ret.push_back(b);
break;
}
return ret;
}
};