1579.Remove Max Number of Edges to Keep Graph Fully Traversable === ###### tags: `Hard`,`Graph` [1579. Remove Max Number of Edges to Keep Graph Fully Traversable](https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable/) ### 題目描述 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]` = [$type_i$, $u_i$, $v_i$] represents a bidirectional edge of type typei between nodes $u_i$ and $v_i$, 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:** ![](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 <= $type_i$ <= 3 * 1 <= $u_i$ < $v_i$ <= `n` * All tuples ($type_i$, $u_i$, $v_i$) are distinct. ### 解答 #### Python ```python= class Solution: def maxNumEdgesToRemove(self, n: int, edges: List[List[int]]) -> int: parents = list(range(n + 1)) def find(x): if parents[x] != x: parents[x] = find(parents[x]) return parents[x] def union(x, y): x, y = find(x), find(y) if x == y: return False parents[x] = y return True ans = alice_count = bob_count = 0 for t, u, v in edges: if t == 3: if union(u, v): alice_count += 1 bob_count += 1 else: ans += 1 temp = parents[:] for t, u, v in edges: if t == 1: if union(u, v): alice_count += 1 else: ans += 1 parents = temp for t, u, v in edges: if t == 2: if union(u, v): bob_count += 1 else: ans += 1 return ans if alice_count == bob_count == n - 1 else -1 ``` > [name=Yen-Chi Chen][time=Mon, May 1, 2023] #### Javascript ```javascript= function maxNumEdgesToRemove(n, edges) { const alice = new UnionFind(n); const bob = new UnionFind(n); let count = 0; for (const [type, v1, v2] of edges) { if (type === 3 && alice.union(v1, v2) && bob.union(v1, v2)) { count++; } } for (const [type, v1, v2] of edges) { if (type === 1 && alice.union(v1, v2)) count++; if (type === 2 && bob.union(v1, v2)) count++; } if (alice.size !== 1 || bob.size !== 1) return -1; return edges.length - count; } class UnionFind { constructor(n) { this.parent = new Array(n + 1).fill().map((_, i) => i); this.size = n; } find(x) { if (x === this.parent[x]) return x; return (this.parent[x] = this.find(this.parent[x])); } union(x, y) { const rootX = this.find(x); const rootY = this.find(y); if (rootX === rootY) return false; this.parent[rootX] = rootY; this.size--; return true; } } ``` > [name=Marsgoat][time=Fri, May 5, 2023] ### Reference [回到題目列表](https://hackmd.io/@Marsgoat/leetcode_every_day)