Easy Solution Of JAVA 🔥C++🔥With Comments 🔥Beginner Friendly 🔥
\n\n# Code\n\nclass Solution {\n  public int maxNumEdgesToRemove(int n, int[][] edges) {\n    if (edges.length < n - 1) return -1; // if edges are less than n -
\n\n# Code\n```\nclass Solution {\n  public int maxNumEdgesToRemove(int n, int[][] edges) {\n    if (edges.length < n - 1) return -1; // if edges are less than n - 1, then it\'s not a valid tree, not all nodes are accessible\n\n    int[] parentAlice = new int[n + 1];\n    for (int i = 0; i < parentAlice.length; i++) {\n      parentAlice[i] = i;\n    }\n\n    // First process the type 3 nodes, this will help us to reduce number of edges required\n    int numType3Edges = 0;\n    for (int[] edge : edges) {\n      if (edge[0] == 3) {\n        if (union(parentAlice, edge[1], edge[2])) {\n          numType3Edges++;\n        }\n      }\n    }\n\n    int[] parentBob = parentAlice.clone(); // as type 3 are for both alice and bob, clone it for bob as well\n\n    // Process Alice\n    int numType1Edges = 0;\n    for (int[] edge : edges) {\n      if (edge[0] == 1) {\n        if (union(parentAlice, edge[1], edge[2])) {\n          numType1Edges++;\n        }\n      }\n    }\n    if (numType1Edges + numType3Edges + 1 != n) return -1; // if edges are less than n - 1, then type 1 is not a valid tree, not all nodes are accessible\n\n    // Process Bob\n    int numType2Edges = 0;\n    for (int[] edge : edges) {\n      if (edge[0] == 2) {\n        if (union(parentBob, edge[1], edge[2])) {\n          numType2Edges++;\n        }\n      }\n    }\n    if (numType2Edges + numType3Edges + 1 != n) return -1; // if edges are less than n - 1, then type 1 is not a valid tree, not all nodes are accessible\n    // return total edges - (number of type 3 + type 2 + type 1 edges required)\n    return edges.length - numType1Edges - numType2Edges - numType3Edges;\n  }\n\n  int find(int[] parent, int node) {\n    if (parent[node] != node) {\n      parent[node] = find(parent, parent[node]);\n    }\n    return parent[node];\n  }\n\n  boolean union(int[] parent, int left, int right) {\n    int leftParent = find(parent, left);\n    int rightParent = find(parent, right);\n    if (leftParent == rightParent) return false;\n    parent[rightParent] = leftParent;\n    return true;\n  }\n}\n```\nC++\n```\nclass Solution {\npublic:\n    int find(int x, vector<int> &parent, vector<int> &rank)\n    {\n        if(x==parent[x]) return x;\n        return parent[x] = find(parent[x], parent, rank);\n    }\n    \n    bool merge(int x, int y, vector<int> &parent, vector<int> &rank)\n    {\n        x = find(x, parent, rank); y = find(y, parent, rank);\n        if(x!=y)\n        {\n            parent[x] = y;\n            if(rank[x]<rank[y]) swap(x,y);\n            rank[x] += rank[y];\n            return true;\n        }\n        return false;\n    }\n    static bool comp(vector<int> &a, vector<int> &b){\n        return a[0]>b[0];\n    }\n    int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {\n        sort(edges.begin(), edges.end(), comp);\n        vector<int> parentA(n+1,-1), rankA(n+1,1), parentB(n+1,-1), rankB(n+1,1);\n        for(int i=1; i<n+1; i++)\n        {\n            parentA[i] = i;\n            parentB[i] = i;\n        }\n        \n        int mergedA = 1, mergedB = 1;\n        int removed = 0;\n        \n        for(auto edge:edges)\n        {\n            int type = edge[0], u = edge[1], v = edge[2];\n            if(type == 3){\n                bool tempa = merge(u,v,parentA,rankA);\n                bool tempb = merge(u,v, parentB, rankB);\n                if(tempa) mergedA++;\n                if(tempb) mergedB++;\n                if(!tempa and !tempb) removed++;\n            }\n            else if(type==1){\n                bool tempa = merge(u,v,parentA,rankA);                \n                if(tempa) mergedA++;\n                else removed++;\n            }\n            else{\n                bool tempb = merge(u,v, parentB, rankB);                \n                if(tempb) mergedB++;\n                else removed++;\n            }\n        }\n        \n        if(mergedA != n || mergedB != n) return -1;\n        return removed;\n    }\n};\n\n```
['Java']