Remove Max Number of Edges to Keep Graph Fully Traversable - Problem

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] = [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.

Input & Output

Example 1 — Basic Graph
$ Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,3],[1,2,4],[1,1,4],[2,3,4]]
Output: 2
💡 Note: Alice needs edges connecting all nodes using type 1 and 3. Bob needs edges using type 2 and 3. The minimum spanning trees need 4 edges total (3 for connectivity + 1 extra for different requirements), so we can remove 6 - 4 = 2 edges.
Example 2 — Impossible Case
$ Input: n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,2,3]]
Output: -1
💡 Note: Alice cannot reach node 4 from nodes 1,2,3 using type 1 and 3 edges. Bob cannot reach node 1 from other nodes using type 2 and 3 edges. Full traversal is impossible.
Example 3 — All Shared Edges
$ Input: n = 3, edges = [[3,1,2],[3,2,3],[3,1,3]]
Output: 1
💡 Note: All edges are type 3 (shared). We need only 2 edges to connect 3 nodes (minimum spanning tree), so we can remove 3 - 2 = 1 edge.

Constraints

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

Visualization

Tap to expand
Remove Max Edges to Keep Graph Traversable INPUT Graph with n=4 nodes 1 2 3 4 Edge Types Type 3: Both Type 1: Alice Type 2: Bob edges = [[3,1,2],[3,2,3], [1,1,3],[1,2,4],[1,1,4],[2,3,4]] n = 4, 6 edges total ALGORITHM STEPS 1 Initialize Union-Find Create UF for Alice and Bob 2 Process Type 3 First Prioritize shared edges Type 3 Processing [3,1,2]: Union 1-2 (used) [3,2,3]: Union 2-3 (used) 2 Type 3 edges added 3 Process Type 1 and 2 Add only if needed [1,1,3]: skip (redundant) [1,2,4]: Union 2-4 Alice [1,1,4]: skip (redundant) [2,3,4]: Union 3-4 Bob 4 Count Removable Total - Used = Removable 6 - 4 = 2 edges removable FINAL RESULT Minimum Spanning Graph 1 2 3 4 Edges Removed [1,1,3] and [1,1,4] (redundant Alice edges) OUTPUT 2 OK - Both can traverse! Key Insight: Use Union-Find with GREEDY selection: Process Type 3 (shared) edges FIRST since they benefit both Alice and Bob. Then add Type 1 and Type 2 edges only when needed. Each traverser needs exactly n-1 edges for a spanning tree. Maximizing shared edges minimizes total edges needed, leaving more edges removable. TutorialsPoint - Remove Max Number of Edges to Keep Graph Fully Traversable | Union-Find with Greedy Edge Selection
Asked in
Google 15 Amazon 12 Microsoft 8
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen