Alice and Bob have an undirected graph of n
nodes and 3 types of edges:
- Type 1: Can be traversed by Alice only.
- Type 2: Can be traversed by Bob only.
- Type 3: Can by 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 it’s impossible for the graph to be fully traversed by Alice and Bob.
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 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:
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:
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 <= edges[i][0] <= 3
1 <= edges[i][1] < edges[i][2] <= n
- All tuples
(typei, ui, vi)
are distinct.
Solution: Greedy + Spanning Tree / Union Find
Use type 3 (both) edges first.
Time complexity: O(E)
Space complexity: O(n)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
class DSU { public: DSU(int n): p_(n + 1), e_(0) { iota(begin(p_), end(p_), 0); } int find(int x) { if (p_[x] == x) return x; return p_[x] = find(p_[x]); } int merge(int x, int y) { int rx = find(x); int ry = find(y); if (rx == ry) return 1; p_[rx] = ry; ++e_; return 0; } int edges() const { return e_; } private: vector<int> p_; int e_; }; class Solution { public: int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) { int ans = 0; DSU A(n), B(n); for (const auto& e: edges) { if (e[0] != 3) continue; ans += A.merge(e[1], e[2]); B.merge(e[1], e[2]); } for (const auto& e: edges) { if (e[0] == 3) continue; DSU& d = e[0] == 1 ? A : B; ans += d.merge(e[1], e[2]); } return (A.edges() == n - 1 && B.edges() == n - 1) ? ans : -1; } }; |
python3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class DSU: def __init__(self, n: int): self.p = list(range(n)) self.e = 0 def find(self, x: int) -> int: if x != self.p[x]: self.p[x] = self.find(self.p[x]) return self.p[x] def merge(self, x: int, y: int) -> int: rx, ry = self.find(x), self.find(y) if rx == ry: return 1 self.p[rx] = ry self.e += 1 return 0 class Solution: def maxNumEdgesToRemove(self, n: int, edges: List[List[int]]) -> int: A, B = DSU(n + 1), DSU(n + 1) ans = 0 for t, x, y in edges: if t != 3: continue ans += A.merge(x, y) B.merge(x, y) for t, x, y in edges: if t == 3: continue d = A if t == 1 else B ans += d.merge(x, y) return ans if A.e == B.e == n - 1 else -1 |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment