Remove Max Number of Edges to Keep Graph Fully Traversable Problem
Description
LeetCode Problem 1579.
Alice and Bob have an undirected graph of n nodes and three types of edges:
- Type 1: Can be traversed by Alice only.
- Type 2: Can be traversed by Bob only.
- Type 3: Can be traversed by both Alice and Bob.
Given an array edges where edges[i] = [type_i, u_i, v_i] represents a bidirectional edge of type type_i between nodes u_i and v_i, 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 Alice and Bob cannot fully traverse the graph.
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^51 <= edges.length <= min(10^5, 3 * n * (n - 1) / 2)edges[i].length == 31 <= type_i <= 31 <= u_i < v_i <= n- All tuples
(type_i, u_i, v_i)are distinct.
Solution
Approach: Union-Find, Shared Edges First
Maximizing the removed edges is the same as keeping as few edges as possible while both Alice's graph (types 1 and 3) and Bob's graph (types 2 and 3) stay connected.
A type 3 edge is worth more than any other: one shared edge can do the job of an Alice-only edge and a Bob-only edge at once. So add the shared edges first, with one union-find structure per person:
- For each type 3 edge, if it joins two different components in Alice's structure, add it to both structures. Up to this point the two structures are identical, so it joins different components for Bob too. If it closes a cycle, it is useless and can be removed.
- Then go through the type 1 edges with Alice's structure and the type 2 edges with Bob's. An edge that joins two components is kept; one that closes a cycle is removed.
At the end, if either structure has more than one component, the person cannot reach every node, so return -1. Otherwise return the number of removed edges.
Why greedy works: each person needs a spanning tree with n - 1 edges. Taking shared edges first builds the largest possible shared forest, so each person needs as few exclusive edges as possible on top of it. Which particular cycle edge gets dropped does not matter, because every spanning forest of a graph has the same number of edges.
Complexity: O((n + E) α(n)) time for E edges, where α is the very slowly growing inverse Ackermann function, and O(n) extra space.
class UnionFind:
def __init__(self, n: int):
self.parent = list(range(n + 1)) # nodes are 1..n
self.size = [1] * (n + 1)
self.components = n
def find(self, x: int) -> int:
while self.parent[x] != x:
self.parent[x] = self.parent[self.parent[x]] # path halving
x = self.parent[x]
return x
def union(self, a: int, b: int) -> bool:
"""Joins the sets of a and b; False if they were already joined (the edge is not needed)."""
a, b = self.find(a), self.find(b)
if a == b:
return False
if self.size[a] < self.size[b]:
a, b = b, a
self.parent[b] = a
self.size[a] += self.size[b]
self.components -= 1
return True
class Solution:
def maxNumEdgesToRemove(self, n: int, edges: List[List[int]]) -> int:
alice, bob = UnionFind(n), UnionFind(n)
removed = 0
# Shared edges first: one of them can replace an Alice-only and a Bob-only edge.
for t, u, v in edges:
if t == 3:
if alice.union(u, v):
bob.union(u, v) # Bob's forest is the same as Alice's so far
else:
removed += 1
for t, u, v in edges:
if t == 1 and not alice.union(u, v):
removed += 1
elif t == 2 and not bob.union(u, v):
removed += 1
if alice.components != 1 or bob.components != 1:
return -1
return removed