Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int parent[1010];
- int root(int u){
- if(parent[u] == u) return u;
- return parent[u] = root(parent[u]);
- }
- void mrg(int u, int v){
- u = root(u);
- v = root(v);
- parent[v] = u;
- }
- vector<int> findRedundantConnection(vector<vector<int>>& edges){
- for(int u=1;u<=1000;u++)
- parent[u] = u;
- int a = -1, b = -1;
- for(auto e: edges){
- int u = e[0];
- int v = e[1];
- if(root(u) == root(v))
- a = u, b = v;
- mrg(u, v);
- }
- return {a, b};
- }
- };
Add Comment
Please, Sign In to add comment