1579. Remove Max Number of Edges to Keep Graph Fully Traversable #
Problem #
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^51 <= edges.length <= min(10^5, 3 * n * (n-1) / 2)edges[i].length == 31 <= edges[i][0] <= 31 <= edges[i][1] < edges[i][2] <= n- All tuples
(typei, ui, vi)are distinct.
Problem Summary #
Alice and Bob share an undirected graph with n nodes and 3 types of edges:
- Type 1: Can only be traversed by Alice.
- Type 2: Can only be traversed by Bob.
- Type 3: Can be 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 that can be removed while ensuring the graph can still be fully traversed by both Alice and Bob. If starting from any node, Alice and Bob can reach all other nodes, the graph is considered fully traversable. Return the maximum number of edges that can be removed. If Alice and Bob cannot fully traverse the graph, return -1.
Solution Approach #
- This problem is an enhanced version of Problem 1319. In Problem 1319, there is only one person, and it also asks for the maximum number of edges that can be removed while ensuring the graph remains connected. This problem simply changes it to 2 people. The solution approach is still Union-Find.
- Initialize 2 Union-Find structures, representing Alice and Bob respectively. First merge the shared edges; for each edge merged, the maximum total number of removable edges decreases by 1. Then merge the individual edges for the 2 people. Similarly, for each edge merged, the maximum total number of removable edges decreases by 1. After merging all edges, if the number of internal sets in either person’s Union-Find is still greater than 1, it means that the 2 people cannot fully traverse the graph, so output -1. If both people’s Union-Find structures each have exactly 1 internal set, it means the entire graph is connected. Output the maximum number of removable edges.
Code #
package leetcode
import (
"github.com/halfrost/leetcode-go/template"
)
func maxNumEdgesToRemove(n int, edges [][]int) int {
alice, bob, res := template.UnionFind{}, template.UnionFind{}, len(edges)
alice.Init(n)
bob.Init(n)
for _, e := range edges {
x, y := e[1]-1, e[2]-1
if e[0] == 3 && (!(alice.Find(x) == alice.Find(y)) || !(bob.Find(x) == bob.Find(y))) {
alice.Union(x, y)
bob.Union(x, y)
res--
}
}
ufs := [2]*template.UnionFind{&alice, &bob}
for _, e := range edges {
if tp := e[0]; tp < 3 && !(ufs[tp-1].Find(e[1]-1) == ufs[tp-1].Find(e[2]-1)) {
ufs[tp-1].Union(e[1]-1, e[2]-1)
res--
}
}
if alice.TotalCount() > 1 || bob.TotalCount() > 1 {
return -1
}
return res
}