-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_684_RedundantConnection.cpp
45 lines (40 loc) · 1.04 KB
/
LC_684_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
/*
684. Redundant Connection
https://leetcode.com/problems/redundant-connection/
*/
class Solution {
vector<int> parent, rank;
int find_set(int x){
if(parent[x] == x) return x; // return parent
return parent[x] = find_set(parent[x]); // travel upward
}
bool union_set(int x, int y)
{
int px = find_set(x);
int py = find_set(y);
if(px == py) return true;
if(rank[px] > rank[py])
swap(px, py);
parent[px] = py;
if(rank[px] == rank[py])
rank[py]++;
return false;
}
public:
vector<int> findRedundantConnection(vector<vector<int>>& edges) {
vector<int> ans;
int n=edges.size();
parent.resize(n, 0); rank.resize(n, 0);
for(int i=0; i<n; i++)
parent[i] = i;
for(auto x: edges)
{
int u = x[0]-1, v = x[1]-1;
if(union_set(u, v))
{
ans = {u+1,v+1};
}
}
return ans;
}
};