C++ | Simple DSU Implementation with explanation
Approach: (Disjoint Set Union)\n\nWe are given with the edges as an input. As every edge connection can be considered as union operation, we do it for every edg
### Approach: (Disjoint Set Union)\n\nWe are given with the edges as an input. As every edge connection can be considered as union operation, we do it for every edge.\n\nAs soon as we encounter an edge between two nodes, which have same parents, that means those nodes are contributing to cycle, hence we got our redundant connection. \n\nMy submission:\n\n```\nclass Solution {\npublic:\n    vector<int> parents;\n    int find_parent(int a){\n        if(parents[a] == a){\n            return a;\n        } else {\n            return parents[a] = find_parent(parents[a]);    //Finding parents using path compression\n        }\n    }\n    void do_union(int node1 , int node2){   \n        node1 = find_parent(node1);\n        node2 = find_parent(node2);\n        if(node1 == node2){     // If they have same parents, do nothing\n            return;\n        } else {                //Else, connect node1 to node2 by assigning the parent of node1 to node2\n            parents[node2] = node1;\n            return;\n        }\n    }\n    vector<int> findRedundantConnection(vector<vector<int>>& edges) {\n        parents = vector<int>(1001);\n        for(int i = 0 ; i < 1001 ; ++i){    //Self initializing the parents, because every node is disconnected at first.\n            parents[i] = i;\n        }\n        vector<int> answer(2 , 0);\n        for(int i = 0 ; i < edges.size() ; ++i){\n            if(find_parent(edges[i][0]) == find_parent(edges[i][1])){   //If parents of these nodes are same, means they already visited/connected, hence contributing in cycle.\n                answer[0] = edges[i][0];\n                answer[1] = edges[i][1];\n                break;\n            } else {\n                do_union(edges[i][0] , edges[i][1]);\n            }\n        }\n        return answer;\n    }\n};\n```\n**PLEASE UPVOTE IT IF YOU LIKED IT, IT REALLY MOTIVATES!! :)**\n
['Union Find', 'C', 'C++']