|
53 | 53 |
|
54 | 54 | <!-- 这里可写通用的实现逻辑 -->
|
55 | 55 |
|
| 56 | +最小生成树问题。 |
| 57 | + |
| 58 | +- 关键边:若删去某条边,导致整个图不连通,或者最小生成树的权值变大,那么这条边就是关键边。 |
| 59 | +- 伪关键边:对于非关键边,我们尝试将该边加入最小生成树集合中,若最小生成树的权值不变,那么这条边就是非关键边。 |
| 60 | + |
| 61 | +**方法一:Kruskal 算法** |
| 62 | + |
| 63 | +先利用 Kruskal 算法,得出最小生成树的权值 v。然后依次枚举每条边,按上面的方法,判断是否是关键边;如果不是关键边,再判断是否是伪关键边。 |
| 64 | + |
56 | 65 | <!-- tabs:start -->
|
57 | 66 |
|
58 | 67 | ### **Python3**
|
59 | 68 |
|
60 | 69 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
61 | 70 |
|
62 | 71 | ```python
|
| 72 | +class UnionFind: |
| 73 | + def __init__(self, n): |
| 74 | + self.p = list(range(n)) |
| 75 | + self.n = n |
| 76 | + |
| 77 | + def union(self, a, b): |
| 78 | + if self.find(a) == self.find(b): |
| 79 | + return False |
| 80 | + self.p[self.find(a)] = self.find(b) |
| 81 | + self.n -= 1 |
| 82 | + return True |
| 83 | + |
| 84 | + def find(self, x): |
| 85 | + if self.p[x] != x: |
| 86 | + self.p[x] = self.find(self.p[x]) |
| 87 | + return self.p[x] |
| 88 | + |
63 | 89 |
|
| 90 | +class Solution: |
| 91 | + def findCriticalAndPseudoCriticalEdges(self, n: int, edges: List[List[int]]) -> List[List[int]]: |
| 92 | + for i, e in enumerate(edges): |
| 93 | + e.append(i) |
| 94 | + edges.sort(key=lambda x: x[2]) |
| 95 | + uf = UnionFind(n) |
| 96 | + v = sum(w for f, t, w, _ in edges if uf.union(f, t)) |
| 97 | + ans = [[], []] |
| 98 | + for f, t, w, i in edges: |
| 99 | + uf = UnionFind(n) |
| 100 | + k = sum(z for x, y, z, j in edges if j != i and uf.union(x, y)) |
| 101 | + if uf.n > 1 or (uf.n == 1 and k > v): |
| 102 | + ans[0].append(i) |
| 103 | + continue |
| 104 | + |
| 105 | + uf = UnionFind(n) |
| 106 | + uf.union(f, t) |
| 107 | + k = w + sum(z for x, y, z, j in edges if j != i and uf.union(x, y)) |
| 108 | + if k == v: |
| 109 | + ans[1].append(i) |
| 110 | + return ans |
64 | 111 | ```
|
65 | 112 |
|
66 | 113 | ### **Java**
|
67 | 114 |
|
68 | 115 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
69 | 116 |
|
70 | 117 | ```java
|
| 118 | +class Solution { |
| 119 | + public List<List<Integer>> findCriticalAndPseudoCriticalEdges(int n, int[][] edges) { |
| 120 | + for (int i = 0; i < edges.length; ++i) { |
| 121 | + int[] e = edges[i]; |
| 122 | + int[] t = new int[4]; |
| 123 | + System.arraycopy(e, 0, t, 0, 3); |
| 124 | + t[3] = i; |
| 125 | + edges[i] = t; |
| 126 | + } |
| 127 | + Arrays.sort(edges, Comparator.comparingInt(a -> a[2])); |
| 128 | + int v = 0; |
| 129 | + UnionFind uf = new UnionFind(n); |
| 130 | + for (int[] e : edges) { |
| 131 | + int f = e[0], t = e[1], w = e[2]; |
| 132 | + if (uf.union(f, t)) { |
| 133 | + v += w; |
| 134 | + } |
| 135 | + } |
| 136 | + List<List<Integer>> ans = new ArrayList<>(); |
| 137 | + for (int i = 0; i < 2; ++i) { |
| 138 | + ans.add(new ArrayList<>()); |
| 139 | + } |
| 140 | + for (int[] e : edges) { |
| 141 | + int f = e[0], t = e[1], w = e[2], i = e[3]; |
| 142 | + uf = new UnionFind(n); |
| 143 | + int k = 0; |
| 144 | + for (int[] ne : edges) { |
| 145 | + int x = ne[0], y = ne[1], z = ne[2], j = ne[3]; |
| 146 | + if (j != i && uf.union(x, y)) { |
| 147 | + k += z; |
| 148 | + } |
| 149 | + } |
| 150 | + if (uf.getN() > 1 || (uf.getN() == 1 && k > v)) { |
| 151 | + ans.get(0).add(i); |
| 152 | + continue; |
| 153 | + } |
| 154 | + uf = new UnionFind(n); |
| 155 | + uf.union(f, t); |
| 156 | + k = w; |
| 157 | + for (int[] ne : edges) { |
| 158 | + int x = ne[0], y = ne[1], z = ne[2], j = ne[3]; |
| 159 | + if (j != i && uf.union(x, y)) { |
| 160 | + k += z; |
| 161 | + } |
| 162 | + } |
| 163 | + if (k == v) { |
| 164 | + ans.get(1).add(i); |
| 165 | + } |
| 166 | + } |
| 167 | + return ans; |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +class UnionFind { |
| 172 | + private int[] p; |
| 173 | + private int n; |
| 174 | + |
| 175 | + public UnionFind(int n) { |
| 176 | + p = new int[n]; |
| 177 | + this.n = n; |
| 178 | + for (int i = 0; i < n; ++i) { |
| 179 | + p[i] = i; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + public int getN() { |
| 184 | + return n; |
| 185 | + } |
| 186 | + |
| 187 | + public boolean union(int a, int b) { |
| 188 | + if (find(a) == find(b)) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + p[find(a)] = find(b); |
| 192 | + --n; |
| 193 | + return true; |
| 194 | + } |
| 195 | + |
| 196 | + public int find(int x) { |
| 197 | + if (p[x] != x) { |
| 198 | + p[x] = find(p[x]); |
| 199 | + } |
| 200 | + return p[x]; |
| 201 | + } |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +### **C++** |
| 206 | + |
| 207 | +```cpp |
| 208 | +class UnionFind { |
| 209 | +public: |
| 210 | + vector<int> p; |
| 211 | + int n; |
| 212 | + |
| 213 | + UnionFind(int _n): n(_n), p(_n) { |
| 214 | + iota(p.begin(), p.end(), 0); |
| 215 | + } |
| 216 | + |
| 217 | + bool unite(int a, int b) { |
| 218 | + if (find(a) == find(b)) return false; |
| 219 | + p[find(a)] = find(b); |
| 220 | + --n; |
| 221 | + return true; |
| 222 | + } |
| 223 | + |
| 224 | + int find(int x) { |
| 225 | + if (p[x] != x) p[x] = find(p[x]); |
| 226 | + return p[x]; |
| 227 | + } |
| 228 | +}; |
| 229 | + |
| 230 | +class Solution { |
| 231 | +public: |
| 232 | + vector<vector<int>> findCriticalAndPseudoCriticalEdges(int n, vector<vector<int>>& edges) { |
| 233 | + for (int i = 0; i < edges.size(); ++i) edges[i].push_back(i); |
| 234 | + sort(edges.begin(), edges.end(), [](auto& a, auto& b) {return a[2] < b[2];}); |
| 235 | + int v = 0; |
| 236 | + UnionFind uf(n); |
| 237 | + for (auto& e : edges) |
| 238 | + { |
| 239 | + int f = e[0], t = e[1], w = e[2]; |
| 240 | + if (uf.unite(f, t)) v += w; |
| 241 | + } |
| 242 | + vector<vector<int>> ans(2); |
| 243 | + for (auto& e : edges) |
| 244 | + { |
| 245 | + int f = e[0], t = e[1], w = e[2], i = e[3]; |
| 246 | + UnionFind ufa(n); |
| 247 | + int k = 0; |
| 248 | + for (auto& ne : edges) |
| 249 | + { |
| 250 | + int x = ne[0], y = ne[1], z = ne[2], j = ne[3]; |
| 251 | + if (j != i && ufa.unite(x, y)) k += z; |
| 252 | + } |
| 253 | + if (ufa.n > 1 || (ufa.n == 1 && k > v)) |
| 254 | + { |
| 255 | + ans[0].push_back(i); |
| 256 | + continue; |
| 257 | + } |
| 258 | + |
| 259 | + UnionFind ufb(n); |
| 260 | + ufb.unite(f, t); |
| 261 | + k = w; |
| 262 | + for (auto& ne : edges) |
| 263 | + { |
| 264 | + int x = ne[0], y = ne[1], z = ne[2], j = ne[3]; |
| 265 | + if (j != i && ufb.unite(x, y)) k += z; |
| 266 | + } |
| 267 | + if (k == v) ans[1].push_back(i); |
| 268 | + } |
| 269 | + return ans; |
| 270 | + } |
| 271 | +}; |
| 272 | +``` |
| 273 | +
|
| 274 | +### **Go** |
| 275 | +
|
| 276 | +```go |
| 277 | +type unionFind struct { |
| 278 | + p []int |
| 279 | + n int |
| 280 | +} |
| 281 | +
|
| 282 | +func newUnionFind(n int) *unionFind { |
| 283 | + p := make([]int, n) |
| 284 | + for i := range p { |
| 285 | + p[i] = i |
| 286 | + } |
| 287 | + return &unionFind{p, n} |
| 288 | +} |
| 289 | +
|
| 290 | +func (uf *unionFind) find(x int) int { |
| 291 | + if uf.p[x] != x { |
| 292 | + uf.p[x] = uf.find(uf.p[x]) |
| 293 | + } |
| 294 | + return uf.p[x] |
| 295 | +} |
| 296 | +
|
| 297 | +func (uf *unionFind) union(a, b int) bool { |
| 298 | + if uf.find(a) == uf.find(b) { |
| 299 | + return false |
| 300 | + } |
| 301 | + uf.p[uf.find(a)] = uf.find(b) |
| 302 | + uf.n-- |
| 303 | + return true |
| 304 | +} |
71 | 305 |
|
| 306 | +func findCriticalAndPseudoCriticalEdges(n int, edges [][]int) [][]int { |
| 307 | + for i := range edges { |
| 308 | + edges[i] = append(edges[i], i) |
| 309 | + } |
| 310 | + sort.Slice(edges, func(i, j int) bool { |
| 311 | + return edges[i][2] < edges[j][2] |
| 312 | + }) |
| 313 | + v := 0 |
| 314 | + uf := newUnionFind(n) |
| 315 | + for _, e := range edges { |
| 316 | + f, t, w := e[0], e[1], e[2] |
| 317 | + if uf.union(f, t) { |
| 318 | + v += w |
| 319 | + } |
| 320 | + } |
| 321 | + ans := make([][]int, 2) |
| 322 | + for _, e := range edges { |
| 323 | + f, t, w, i := e[0], e[1], e[2], e[3] |
| 324 | + uf = newUnionFind(n) |
| 325 | + k := 0 |
| 326 | + for _, ne := range edges { |
| 327 | + x, y, z, j := ne[0], ne[1], ne[2], ne[3] |
| 328 | + if j != i && uf.union(x, y) { |
| 329 | + k += z |
| 330 | + } |
| 331 | + } |
| 332 | + if uf.n > 1 || (uf.n == 1 && k > v) { |
| 333 | + ans[0] = append(ans[0], i) |
| 334 | + continue |
| 335 | + } |
| 336 | + uf = newUnionFind(n) |
| 337 | + uf.union(f, t) |
| 338 | + k = w |
| 339 | + for _, ne := range edges { |
| 340 | + x, y, z, j := ne[0], ne[1], ne[2], ne[3] |
| 341 | + if j != i && uf.union(x, y) { |
| 342 | + k += z |
| 343 | + } |
| 344 | + } |
| 345 | + if k == v { |
| 346 | + ans[1] = append(ans[1], i) |
| 347 | + } |
| 348 | + } |
| 349 | + return ans |
| 350 | +} |
72 | 351 | ```
|
73 | 352 |
|
74 | 353 | ### **...**
|
|
0 commit comments