2
public int maxNumEdgesToRemove(int n, int[][] edges) {7
}); // giving the priority to third type of edge or the edge which Bob and Alice both can10
// 1-based indexing of nodes11
int[] parentAlice = new int[n + 1]; // Graph 1 for Alice connectedness12
int[] parentBob = new int[n + 1]; // Graph 2 for Bob connectedness16
i++) { // every node is pointing to itself, at first no connection is considered all sets17
// are independent(no dependency)22
// number of merged unique node for Alice and Bob that are required to maintain the23
// connectedness of Alice and Bob graph nodes//intialised with one because merging happens in28
// number of cyclic or the non dependent node, that are not required for the connectedness of29
// Alice and Bob nodes32
for (int[] edge : edges) {35
0]; // category of edge 1)edge Alice can only access 2)edge Bob can only access 3)edge36
// both Alice and Bob can access40
if (cat == 3) { // edge both Alice and Bob an access42
// creating dependency of nodes in graph 1 and 243
boolean tempAlice = union(u, v, parentAlice);44
boolean tempBob = union(u, v, parentBob);46
if (tempAlice == true) mergeAlice += 1;48
if (tempBob == true) mergeBob += 1;50
if (tempAlice == false52
== false) // retundant or the cyclic non-dependent edge//both Alice and Bob don't53
// rquire it connection is already there between these pair of nodes55
} else if (cat == 2) { // edge Bob can only access57
// creating dependency of nodes in graph 258
boolean tempBob = union(u, v, parentBob);60
if (tempBob == true) mergeBob += 1;61
else // no merging of set is done, that means that this edge is not required because it will62
// form cycle or the dependency64
} else { // edge Alice can only access66
// creating dependency of nodes in graph 167
boolean tempAlice = union(u, v, parentAlice);69
if (tempAlice == true) mergeAlice += 1;70
else // no merging of set is done, that means that this edge is not required because it will71
// form cycle or the dependency76
|| mergeBob != n) // all node are not connected, connectedness is not maintained78
return removeEdge; // number of edge removed by maintaining the connectedness81
public int find(int x, int[] parent) {82
if (parent[x] == x) // when we found the absolute root or the leader of the set85
int temp = find(parent[x], parent);88
temp; // Path Compression//child pointing to the absolute root or the leader of the set,91
return temp; // returning the absolute root94
public boolean union(int x, int y, int[] parent) {95
int lx = find(x, parent); // leader of set x or the absolute root96
int ly = find(y, parent); // leader of set y or the absolute root98
if (lx != ly) { // belong to different set merging100
// Rank Compression is not done, but you can do it103
return true; // union done, dependency created104
} else return false; // no union done cycle is due to this edge105
} // Please do Upvote, it helps a lot