⭐️⭐️⭐️
# 題目敘述
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.
# 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.
# Solution
class UnionFind { | |
public: | |
vector<int> parent, rank; | |
int nodes; | |
UnionFind(int n): parent(n+1), rank(n+1), nodes(1) { | |
for (int i=1; i<=n; i++) parent[i] = i; | |
} | |
int find (int x) { | |
if (x == parent[x]) return x; | |
return parent[x] = find(parent[x]); | |
} | |
bool union_(int x, int y) { | |
int px = find(x); | |
int py = find(y); | |
if (px == py) return false; | |
if (rank[px] > rank[py]) parent[py] = px; | |
else if (rank[px] < rank[py]) parent[px] = py; | |
else { | |
parent[py] = px; | |
rank[px]++; | |
} | |
return true; | |
} | |
}; | |
class Solution { | |
public: | |
int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) { | |
UnionFind Alice(n); | |
UnionFind Bob(n); | |
int res = 0; | |
for (vector<int>& edge : edges) { | |
if (edge[0] == 3) { | |
if (Alice.union_(edge[1], edge[2]) && Bob.union_(edge[1], edge[2])) { | |
Alice.nodes += 1; | |
Bob.nodes += 1; | |
} else { | |
res += 1; | |
} | |
} | |
} | |
for (auto& edge : edges) { | |
if (edge[0] == 1) { | |
if (Alice.union_(edge[1], edge[2])) Alice.nodes += 1; | |
else res += 1; | |
} | |
if (edge[0] == 2) { | |
if (Bob.union_(edge[1], edge[2])) Bob.nodes += 1; | |
else res += 1; | |
} | |
} | |
return (Alice.nodes == Bob.nodes && Alice.nodes == n) ? res : -1; | |
} | |
}; |
class Solution { | |
class UnionFind { | |
int[] parent, rank; | |
int nodes; | |
public UnionFind(int n) { | |
parent = new int[n+1]; | |
rank = new int[n+1]; | |
nodes = 1; | |
for (int i=1; i<=n; i++) parent[i] = i; | |
} | |
public int find(int x) { | |
if (parent[x] != x) parent[x] = find(parent[x]); | |
return parent[x]; | |
} | |
public boolean union_(int x, int y) { | |
int px = find(x); | |
int py = find(y); | |
if (px == py) return false; | |
if (rank[px] > rank[py]) parent[py] = px; | |
else if (rank[px] < rank[py]) parent[px] = py; | |
else { | |
parent[py] = px; | |
rank[px] += 1; | |
} | |
return true; | |
} | |
} | |
public int maxNumEdgesToRemove(int n, int[][] edges) { | |
UnionFind Alice = new UnionFind(n); | |
UnionFind Bob = new UnionFind(n); | |
int res = 0; | |
for (int[] edge : edges) { | |
if (edge[0] == 3) { | |
if (Alice.union_(edge[1], edge[2]) && Bob.union_(edge[1], edge[2])) { | |
Alice.nodes += 1; | |
Bob.nodes += 1; | |
} else res += 1; | |
} | |
} | |
for (int[] edge : edges) { | |
if (edge[0] == 1) { | |
if (Alice.union_(edge[1], edge[2])) Alice.nodes += 1; | |
else res += 1; | |
} | |
if (edge[0] == 2) { | |
if (Bob.union_(edge[1], edge[2])) Bob.nodes += 1; | |
else res += 1; | |
} | |
} | |
return Alice.nodes == Bob.nodes && Alice.nodes == n ? res : -1; | |
} | |
} |
class UnionFind: | |
def __init__(self, n): | |
self.parent = list(range(n+1)) | |
self.rank = [0] * (n+1) | |
self.nodes = 1 | |
def find(self, x): | |
if self.parent[x] != x: | |
self.parent[x] = self.find(self.parent[x]) | |
return self.parent[x] | |
def union_(self, x, y): | |
px = self.find(x) | |
py = self.find(y) | |
if px == py: return False | |
if self.rank[px] > self.rank[py]: self.parent[py] = px | |
elif self.rank[px] < self.rank[py]: self.parent[px] = py | |
else: | |
self.parent[py] = px | |
self.rank[px] += 1 | |
return True | |
class Solution: | |
def maxNumEdgesToRemove(self, n: int, edges: List[List[int]]) -> int: | |
Alice = UnionFind(n) | |
Bob = UnionFind(n) | |
res = 0 | |
for t, i, j in edges: | |
if t == 3: | |
if Alice.union_(i, j) and Bob.union_(i, j): | |
Alice.nodes += 1 | |
Bob.nodes += 1 | |
else: res += 1 | |
for t, i, j in edges: | |
if t == 1: | |
if Alice.union_(i, j): Alice.nodes += 1 | |
else: res += 1 | |
if t == 2: | |
if Bob.union_(i, j): Bob.nodes += 1 | |
else: res += 1 | |
return res if Alice.nodes == Bob.nodes == n else -1 |