Redundant Connection II in C++


Suppose we have a rooted tree. This is a directed graph such that, there is exactly one node (which is the root) for which all other nodes are descendants of this node, and every node has exactly one parent, except for the root node. Root has no parents.

In the given input a directed graph that started as a rooted tree with N nodes (All values are unique), with one additional directed edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.

Graph will be a 2D-array of edges. Each element of edges is a pair like [u, v] that represents a directed edge connecting nodes u and v, where u is a parent of child v.

We have to find an edge that can be removed so that the resulting graph is a rooted tree of N nodes. There may be multiple answers, we have to return the answer that occurs last in the given 2D-array.

So if the input is like −

The output will be [2,3]

To solve this, we will follow these steps −

  • Define a function getParent(), this will take node, an array parent,
  • if parent[node] is same as -1, then −
    • return node
  • return parent[node] = getParent(parent[node], parent)
  • From the main method, do the following −
  • n := size of edges
  • Define an array parent of size n + 5, fill this with -1
  • Define an array ds of size n + 5 fill this with -1
  • last := -1, second := - 1, first := - 1
  • for initialize i := 0, when i < n, update (increase i by 1), do −
    • u := edges[i, 0], v := edges[i, 1]
    • if parent[v] is not equal to -1, then −
      • first := parent[v]
      • second := i
      • Ignore following part, skip to the next iteration
    • parent[v] := i, parentU := getParent(u, ds), parentV := getParent(v, ds)
    • if parentU is same as parentV, then −
      • last := i
    • Otherwise,
      • ds[parentV] := parentU
  • if last is same as -1, then −
    • return edges[second]
  • if second is same as -1, then −
    • return edges[last]
  • return edges[first]

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class Solution {
public:
   int getParent(int node, vector <int>& parent){
      if(parent[node] == -1)return node;
      return parent[node] = getParent(parent[node], parent);
   }
   vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
      int n = edges.size();
      vector <int> parent(n + 5, -1);
      vector <int> ds(n + 5, -1);
      int last = -1, second = -1, first = -1;
      int u, v;
      int parentU, parentV;
      for(int i = 0; i < n; i++){
         u = edges[i][0];
         v = edges[i][1];
         if(parent[v] != -1){
            first = parent[v];
            second = i;
            continue;
         }
         parent[v] = i;
         parentU = getParent(u, ds);
         parentV = getParent(v, ds);
         if(parentU == parentV){
            last = i;
         }else ds[parentV] = parentU;
      }
      if(last == -1)return edges[second];
      if(second == -1)return edges[last];
      return edges[first];
   }
};
main(){
   Solution ob;
   vector<vector<int>> v = {{1,2},{1,3},{2,3}};
   print_vector(ob.findRedundantDirectedConnection(v));
}

Input

{{1,2},{1,3},{2,3}}

Output

[2, 3, ]

Updated on: 01-Jun-2020

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements