Formatted question description: https://leetcode.ca/all/1579.html

# 1579. Remove Max Number of Edges to Keep Graph Fully Traversable (Hard)

Alice and Bob have an undirected graph of n nodes and 3 types of edges:

• Type 1: Can be traversed by Alice only.
• Type 2: Can be traversed by Bob only.
• Type 3: Can by traversed by both Alice and Bob.

Given an array edges where edges[i] = [typei, ui, vi] represents a bidirectional edge of type typei between nodes ui and vi, find the maximum number of edges you can remove so that after removing the edges, the graph can still be fully traversed by both Alice and Bob. The graph is fully traversed by Alice and Bob if starting from any node, they can reach all other nodes.

Return the maximum number of edges you can remove, or return -1 if it's impossible for the graph to be fully traversed by Alice and Bob.

Example 1: Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,2],[2,3,4]]
Output: 2
Explanation: If we remove the 2 edges [1,1,2] and [1,1,3]. The graph will still be fully traversable by Alice and Bob. Removing any additional edge will not make it so. So the maximum number of edges we can remove is 2.


Example 2: Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]
Output: 0
Explanation: Notice that removing any edge will not make the graph fully traversable by Alice and Bob.


Example 3: Input: n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]
Output: -1
Explanation: In the current graph, Alice cannot reach node 4 from the other nodes. Likewise, Bob cannot reach 1. Therefore it's impossible to make the graph fully traversable.

Constraints:

• 1 <= n <= 10^5
• 1 <= edges.length <= min(10^5, 3 * n * (n-1) / 2)
• edges[i].length == 3
• 1 <= edges[i] <= 3
• 1 <= edges[i] < edges[i] <= n
• All tuples (typei, ui, vi) are distinct.

Related Topics:
Union Find

## Solution 1. Union Find

Create two UnionFinds a and b for Alice and Bob respectively.

We should take type 3 edges first, then type 1 and 2.

If two nodes are already connected, we increment the answer ans. Otherwise we connect them.

In the end, if a.getSize() == 1 && b.size() == 1 meaning that the graph is all connected for both Alice and Bob, we return ans; otherwise return -1.

// OJ: https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/

// Time: O(N + E)
// Space: O(N)
class UnionFind{
vector<int> id;
int size;
public:
UnionFind(int N ): id(N), size(N) {
iota(begin(id), end(id), 0);
}
int find(int a) {
return id[a] == a ? a : (id[a] = find(id[a]));
}
int connected(int a, int b) {
return find(a) == find(b);
}
void connect(int a, int b) {
int p = find(a), q = find(b);
if (p == q) return;
id[p] = q;
--size;
}
int getSize() { return size; }
};
class Solution {
public:
int maxNumEdgesToRemove(int n, vector<vector<int>>& E) {
UnionFind a(n), b(n);
int ans = 0;
for (auto &e : E) {
if (e != 3) continue;
int u = e - 1, v = e - 1;
if (a.connected(u, v)) {
++ans;
continue;
}
a.connect(u, v);
b.connect(u, v);
}
for (auto &e : E) {
int u = e - 1, v = e - 1;
if (e == 1) {
if (a.connected(u, v)) ++ans;
else a.connect(u, v);
} else if (e == 2) {
if (b.connected(u, v)) ++ans;
else b.connect(u, v);
}
}
return a.getSize() != 1 || b.getSize() != 1 ? -1 : ans;
}
};


Java

class Solution {
int[] parents1;
int[] parents2;
int[] parents3;

public int maxNumEdgesToRemove(int n, int[][] edges) {
parents1 = new int[n + 1];
parents2 = new int[n + 1];
parents3 = new int[n + 1];
for (int i = 1; i <= n; i++) {
parents1[i] = i;
parents2[i] = i;
parents3[i] = i;
}
int count = 0, count1 = 0, count2 = 0;
for (int[] edge : edges) {
int type = edge, node1 = edge, node2 = edge;
if (type == 3) {
int root1 = find(parents3, node1);
int root2 = find(parents3, node2);
if (root1 != root2) {
parents3[root1] = root2;
count++;
count1++;
count2++;
root1 = find(parents1, node1);
root2 = find(parents1, node2);
parents1[root1] = root2;
root1 = find(parents2, node1);
root2 = find(parents2, node2);
parents2[root1] = root2;
}
}
}
for (int[] edge : edges) {
int type = edge, node1 = edge, node2 = edge;
if (type == 1) {
int root1 = find(parents1, node1);
int root2 = find(parents1, node2);
if (root1 != root2) {
count++;
count1++;
parents1[root1] = root2;
}
} else if (type == 2) {
int root1 = find(parents2, node1);
int root2 = find(parents2, node2);
if (root1 != root2) {
count++;
count2++;
parents2[root1] = root2;
}
}
}
if (count1 != n - 1 || count2 != n - 1)
return -1;
return edges.length - count;
}

public int find(int[] parents, int index) {
if (parents[index] == index)
return index;
else {
parents[index] = find(parents, parents[index]);
return parents[index];
}
}
}