Alice and Bob have an undirected graph of n nodes and three types of edges:
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 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 <= 1051 <= edges.length <= min(105, 3 * n * (n - 1) / 2)edges[i].length == 31 <= typei <= 31 <= ui < vi <= n(typei, ui, vi) are distinct.This approach utilizes the Union-Find (or Disjoint Set Union, DSU) data structure to manage the connectivity of nodes. The critical idea is to process edge types in a specific order to ensure that both Alice and Bob can fully traverse the graph while maximizing the number of removable edges:
This code defines a struct DSU representing the Disjoint-Set Union data structure for managing connected components. The functions createDSU, find, and unionSet handle initialization, set finding, and union operations, respectively.
The main function, maxNumEdgesToRemove, processes the edges by type. Type 3 edges are processed first because they serve both Alice and Bob. Then type 1 and 2 edges are processed separately for Alice’s and Bob’s connectivity. The variable savedEdges stores the number of redundant edges found. After processing, checking the number of connected components in alice and bob determines if the graph is fully traversable, and returns the maximum number of removable edges.
C++
Java
Python
C#
JavaScript
Time Complexity: O(E * α(n)), where E is the number of edges and α(n) is the inverse Ackermann function, effectively constant.
Space Complexity: O(n) for storing the disjoint sets for both Alice and Bob.
Remove Max Number of Edges to Keep Graph Fully Traversable - Leetcode 1579 - Python • NeetCodeIO • 12,354 views views
Watch 9 more video solutions →Practice Remove Max Number of Edges to Keep Graph Fully Traversable with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor