Skip to content

Commit 02a22c4

Browse files
committed
feat: update solutions to lc problem: No.1579
No.1579.Remove Max Number of Edges to Keep Graph Fully Traversable
1 parent 7052dc2 commit 02a22c4

File tree

6 files changed

+116
-190
lines changed

6 files changed

+116
-190
lines changed

solution/1500-1599/1579.Remove Max Number of Edges to Keep Graph Fully Traversable/README.md

+43-68
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,13 @@
6363

6464
<!-- 这里可写通用的实现逻辑 -->
6565

66-
并查集。
66+
并查集。对于本题,构造两个并查集。同时操作两个并查集 ufa, ufb,遵从优先添加公共边的策略,即:优先添加 `type = 3` 的边。添加的过程中,若两点已连通,则累加多余的边 ans。
67+
68+
然后遍历 `type = 1` 的边添加到 ufa 中,而 `type = 2` 的边则添加到 ufb 中。此过程同样判断两点是否已连通,若是,则累加多余的边 ans。
69+
70+
最后判断两个并查集是否都只有一个连通分量,若是,返回 ans,否则返回 -1。
71+
72+
以下是并查集的几个常用模板。
6773

6874
模板 1——朴素并查集:
6975

@@ -116,12 +122,6 @@ p[find(a)] = find(b)
116122
d[find(a)] = distance
117123
```
118124

119-
对于本题,构造两个并查集。同时操作两个并查集 ufa, ufb,优先添加 `type = 3` 的边,添加的过程中,若两点已连通,则累加多余的边 res。
120-
121-
然后遍历 `type = 1` 的边添加到 ufa 中,而 `type = 2` 的边则添加到 ufb 中。此过程同样判断两点是否已连通,若是,则累加多余的边 res。
122-
123-
最后判断两个并查集是否都只有一个连通分量,若是,返回 res,否则返回 -1。
124-
125125
<!-- tabs:start -->
126126

127127
### **Python3**
@@ -134,37 +134,33 @@ class UnionFind:
134134
self.p = list(range(n))
135135
self.n = n
136136

137-
def union(self, a, b) -> bool:
137+
def union(self, a, b):
138138
pa, pb = self.find(a - 1), self.find(b - 1)
139139
if pa == pb:
140140
return False
141141
self.p[pa] = pb
142142
self.n -= 1
143143
return True
144144

145-
def find(self, x) -> int:
145+
def find(self, x):
146146
if self.p[x] != x:
147147
self.p[x] = self.find(self.p[x])
148148
return self.p[x]
149149

150+
150151
class Solution:
151152
def maxNumEdgesToRemove(self, n: int, edges: List[List[int]]) -> int:
152153
ufa, ufb = UnionFind(n), UnionFind(n)
153-
res = 0
154+
ans = 0
154155
for t, u, v in edges:
155156
if t == 3:
156-
if not ufa.union(u, v):
157-
res += 1
158-
else:
157+
if ufa.union(u, v):
159158
ufb.union(u, v)
160-
for t, u, v in edges:
161-
if t == 1:
162-
if not ufa.union(u, v):
163-
res += 1
164-
elif t == 2:
165-
if not ufb.union(u, v):
166-
res += 1
167-
return res if ufa.n == 1 and ufb.n == 1 else -1
159+
else:
160+
ans += 1
161+
ans += sum((t == 1 and not ufa.union(u, v))
162+
or (t == 2 and not ufb.union(u, v)) for t, u, v in edges)
163+
return ans if ufa.n == 1 and ufb.n == 1 else -1
168164
```
169165

170166
### **Java**
@@ -176,28 +172,22 @@ class Solution {
176172
public int maxNumEdgesToRemove(int n, int[][] edges) {
177173
UnionFind ufa = new UnionFind(n);
178174
UnionFind ufb = new UnionFind(n);
179-
int res = 0;
175+
int ans = 0;
180176
for (int[] e : edges) {
181177
if (e[0] == 3) {
182-
if (!ufa.union(e[1], e[2])) {
183-
++res;
184-
} else {
178+
if (ufa.union(e[1], e[2])) {
185179
ufb.union(e[1], e[2]);
180+
} else {
181+
++ans;
186182
}
187183
}
188184
}
189185
for (int[] e : edges) {
190-
if (e[0] == 1) {
191-
if (!ufa.union(e[1], e[2])) {
192-
++res;
193-
}
194-
} else if (e[0] == 2) {
195-
if (!ufb.union(e[1], e[2])) {
196-
++res;
197-
}
186+
if ((e[0] == 1 && !ufa.union(e[1], e[2])) || (e[0] == 2 && !ufb.union(e[1], e[2]))) {
187+
++ans;
198188
}
199189
}
200-
return ufa.n == 1 && ufb.n == 1 ? res : -1;
190+
return ufa.n == 1 && ufb.n == 1 ? ans : -1;
201191
}
202192
}
203193

@@ -214,7 +204,8 @@ class UnionFind {
214204
}
215205

216206
public boolean union(int a, int b) {
217-
int pa = find(a - 1), pb = find(b - 1);
207+
int pa = find(a - 1);
208+
int pb = find(b - 1);
218209
if (pa == pb) {
219210
return false;
220211
}
@@ -246,16 +237,14 @@ public:
246237

247238
bool unite(int a, int b) {
248239
int pa = find(a - 1), pb = find(b - 1);
249-
if (pa == pb)
250-
return false;
240+
if (pa == pb) return false;
251241
p[pa] = pb;
252242
--n;
253243
return true;
254244
}
255245

256246
int find(int x) {
257-
if (p[x] != x)
258-
p[x] = find(p[x]);
247+
if (p[x] != x) p[x] = find(p[x]);
259248
return p[x];
260249
}
261250
};
@@ -264,27 +253,19 @@ class Solution {
264253
public:
265254
int maxNumEdgesToRemove(int n, vector<vector<int>>& edges) {
266255
UnionFind ufa(n), ufb(n);
267-
int res = 0;
268-
for (auto e : edges)
256+
int ans = 0;
257+
for (auto& e : edges)
269258
{
270259
if (e[0] == 3)
271260
{
272-
if (!ufa.unite(e[1], e[2])) ++res;
273-
else ufb.unite(e[1], e[2]);
261+
if (ufa.unite(e[1], e[2])) ufb.unite(e[1], e[2]);
262+
else ++ans;
274263
}
275264
}
276-
for (auto e : edges)
277-
{
278-
if (e[0] == 1)
279-
{
280-
if (!ufa.unite(e[1], e[2])) ++res;
281-
}
282-
else if (e[0] == 2)
283-
{
284-
if (!ufb.unite(e[1], e[2])) ++res;
285-
}
286-
}
287-
return ufa.n == 1 && ufb.n == 1 ? res : -1;
265+
for (auto& e : edges)
266+
if ((e[0] == 1 && !ufa.unite(e[1], e[2])) || (e[0] == 2 && !ufb.unite(e[1], e[2])))
267+
++ans;
268+
return ufa.n == 1 && ufb.n == 1 ? ans : -1;
288269
}
289270
};
290271
```
@@ -324,29 +305,23 @@ func (uf *unionFind) union(a, b int) bool {
324305
325306
func maxNumEdgesToRemove(n int, edges [][]int) int {
326307
ufa, ufb := newUnionFind(n), newUnionFind(n)
327-
res := 0
308+
ans := 0
328309
for _, e := range edges {
329310
if e[0] == 3 {
330-
if !ufa.union(e[1], e[2]) {
331-
res++
332-
} else {
311+
if ufa.union(e[1], e[2]) {
333312
ufb.union(e[1], e[2])
313+
} else {
314+
ans++
334315
}
335316
}
336317
}
337318
for _, e := range edges {
338-
if e[0] == 1 {
339-
if !ufa.union(e[1], e[2]) {
340-
res++
341-
}
342-
} else if e[0] == 2 {
343-
if !ufb.union(e[1], e[2]) {
344-
res++
345-
}
319+
if (e[0] == 1 && !ufa.union(e[1], e[2])) || (e[0] == 2 && !ufb.union(e[1], e[2])) {
320+
ans++
346321
}
347322
}
348323
if ufa.n == 1 && ufb.n == 1 {
349-
return res
324+
return ans
350325
}
351326
return -1
352327
}

0 commit comments

Comments
 (0)