1579. Remove Max Number of Edges to Keep Graph Fully Traversable
Tags: php, leetcode, algorithms, programming
Published: 2024-06-30 18:27:16

1579\. Remove Max Number of Edges to Keep Graph Fully Traversable

Hard

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 <code>edges[i] = [type<sub>i</sub>, u<sub>i</sub>, v<sub>i</sub>]</code> represents a bidirectional edge of type <code>type<sub>i</sub></code> between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>, 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:**

![ex1](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:**

![ex2](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:**

![ex3](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:**


- <code>1 <= n <= 10<sup>5</sup></code>
- <code>1 <= edges.length <= min(10<sup>5</sup>, 3 * n * (n - 1) / 2)</code>
- <code>edges[i].length == 3</code>
- <code>1 <= type<sub>i</sub> <= 3</code>
- <code>1 <= u<sub>i</sub> < v<sub>i</sub> <= n</code>
- All tuples <code>(type<sub>i</sub>, u<sub>i</sub>, v<sub>i</sub>)</code> are distinct.


**Solution:**

```
class UnionFind {
    private $parent;
    private $rank;

    public function __construct($n) {
        $this->parent = range(0, $n);
        $this->rank = array_fill(0, $n + 1, 1);
    }

    public function find($x) {
        if ($this->parent[$x] !== $x) {
            $this->parent[$x] = $this->find($this->parent[$x]);
        }
        return $this->parent[$x];
    }

    public function union($x, $y) {
        $rootX = $this->find($x);
        $rootY = $this->find($y);

        if ($rootX === $rootY) {
            return false;
        }

        if ($this->rank[$rootX] > $this->rank[$rootY]) {
            $this->parent[$rootY] = $rootX;
        } elseif ($this->rank[$rootX] < $this->rank[$rootY]) {
            $this->parent[$rootX] = $rootY;
        } else {
            $this->parent[$rootY] = $rootX;
            $this->rank[$rootX]++;
        }

        return true;
    }
}

class Solution {

    /**
     * @param Integer $n
     * @param Integer[][] $edges
     * @return Integer
     */
    function maxNumEdgesToRemove($n, $edges) {
        $ufAlice = new UnionFind($n);
        $ufBob = new UnionFind($n);

        usort($edges, function($a, $b) {
            return $b[0] - $a[0];
        });

        $requiredEdges = 0;

        foreach ($edges as $edge) {
            $type = $edge[0];
            $u = $edge[1];
            $v = $edge[2];

            if ($type === 3) {
                $unionAlice = $ufAlice->union($u, $v);
                $unionBob = $ufBob->union($u, $v);
                if ($unionAlice || $unionBob) {
                    $requiredEdges++;
                }
            } elseif ($type === 1) {
                if ($ufAlice->union($u, $v)) {
                    $requiredEdges++;
                }
            } elseif ($type === 2) {
                if ($ufBob->union($u, $v)) {
                    $requiredEdges++;
                }
            }
        }

        for ($i = 2; $i <= $n; $i++) {
            if ($ufAlice->find($i) !== $ufAlice->find(1) || $ufBob->find($i) !== $ufBob->find(1)) {
                return -1;
            }
        }

        return count($edges) - $requiredEdges;
    }
}
```



**Contact Links**

- **[LinkedIn](https://www.linkedin.com/in/arifulhaque/)**
- **[GitHub](https://github.com/mah-shamim)**