-
Notifications
You must be signed in to change notification settings - Fork 0
/
redundant-connection.cpp
56 lines (54 loc) · 989 Bytes
/
redundant-connection.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
/**
* Author: Vindru
* Problem: (https://leetcode.com/problems/redundant-connection/)
*
*
*/
class Solution
{
public:
vector<int> findRedundantConnection(vector<vector<int> > &edges)
{
vector<int> base;
for (int i = 0; i <= edges.size(); i++)
{
base.push_back(i);
}
vector<int> res;
for (int i = 0; i < edges.size(); i++)
{
int x = edges[i][0];
int y = edges[i][1];
if (base[x] == base[y])
{
res = edges[i];
}
else
{
int m = base[y];
int n = base[x];
if (x < y)
{
for (int i = 0; i <= edges.size(); i++)
{
if (m == base[i])
{
base[i] = n;
}
}
}
else
{
for (int i = 0; i <= edges.size(); i++)
{
if (n == base[i])
{
base[i] = m;
}
}
}
}
}
return res;
}
};