> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Remove Max Number of Edges to Keep Graph

> Tested Python solution for LeetCode 1579 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1579, [Hard](/catalog/hard). Topics: [Union-Find](/catalog/topics/union-find), [Graph Theory](/catalog/topics/graph-theory). [View on LeetCode](https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1579   # by problem number
lcpy gen -s remove_max_number_of_edges_to_keep_graph_fully_traversable   # by problem name
```

## 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.*

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/08/19/ex1.png)

```
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](https://assets.leetcode.com/uploads/2020/08/19/ex2.png)

```
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](https://assets.leetcode.com/uploads/2020/08/19/ex3.png)

```
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^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.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/remove_max_number_of_edges_to_keep_graph_fully_traversable/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/remove_max_number_of_edges_to_keep_graph_fully_traversable/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class DSU:
    def __init__(self, size: int) -> None:
        self.parent = list(range(size))

    def find(self, x: int) -> int:
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x

    def union(self, a: int, b: int) -> bool:
        root_a, root_b = self.find(a), self.find(b)
        if root_a == root_b:
            return False
        self.parent[root_a] = root_b
        return True


class Solution:
    # Time: O(e * alpha(n))
    # Space: O(n)
    def max_num_edges_to_remove(self, n: int, edges: list[list[int]]) -> int:
        alice = DSU(n + 1)
        bob = DSU(n + 1)
        kept = 0

        for edge_type, u, v in edges:
            if edge_type == 3:
                merged_alice = alice.union(u, v)
                merged_bob = bob.union(u, v)
                if merged_alice or merged_bob:
                    kept += 1

        for edge_type, u, v in edges:
            merged = (edge_type == 1 and alice.union(u, v)) or (edge_type == 2 and bob.union(u, v))
            if merged:
                kept += 1

        if len({alice.find(node) for node in range(1, n + 1)}) > 1:
            return -1
        if len({bob.find(node) for node in range(1, n + 1)}) > 1:
            return -1
        return len(edges) - kept
```

## Complexity

| Time | Space |
| - | - |
| O(e \* alpha(n)) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.