Skip to content

Practice Remove Max Number of Edges to Keep Graph Fully Traversable

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1579
LeetCode Problem

Remove Max Number of Edges to Keep Graph Fully Traversable

Problem Statement

The undirected graph has n nodes and edges [type,u,v]. Type 1 is usable only by Alice, type 2 only by Bob, and type 3 by either. Remove as many edges as possible while keeping every node reachable from every other node separately for each person using their permitted edges. Return that maximum removal count, or -1 if it cannot be achieved.

Source: Remove Max Number of Edges to Keep Graph Fully Traversable on LeetCode. Use the original task for its coding environment and submissions.

Examples

# 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

# Example 2

Input
n = 4, edges = [[3,1,2],[3,2,3],[1,1,4],[2,1,4]]
Output
0

# Example 3

Input
n = 4, edges = [[3,2,3],[1,1,2],[2,3,4]]
Output
-1

Constraints

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

Related LeetCode Problems

Practice more problems similar to Remove Max Number of Edges to Keep Graph Fully Traversable.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.