|
45 | 45 |
|
46 | 46 | <!-- 这里可写通用的实现逻辑 -->
|
47 | 47 |
|
| 48 | +**方法一:并查集** |
| 49 | + |
| 50 | +有两个入度时,当一条边被记为 conflict,就相当于删掉了这条边,因为并没有调用并查集 union 进行合并,如果还出现了无向环,则说明是要删另一条入度的边。 |
| 51 | + |
| 52 | +每个节点都只有一个入度时,则说明是一个有向环,删最后一条出现的边即可。 |
| 53 | + |
48 | 54 | <!-- tabs:start -->
|
49 | 55 |
|
50 | 56 | ### **Python3**
|
51 | 57 |
|
52 | 58 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
53 | 59 |
|
54 | 60 | ```python
|
| 61 | +class UnionFind: |
| 62 | + def __init__(self, n): |
| 63 | + self.p = list(range(n)) |
| 64 | + self.n = n |
| 65 | + |
| 66 | + def union(self, a, b): |
| 67 | + if self.find(a) == self.find(b): |
| 68 | + return False |
| 69 | + self.p[self.find(a)] = self.find(b) |
| 70 | + self.n -= 1 |
| 71 | + return True |
| 72 | + |
| 73 | + def find(self, x): |
| 74 | + if self.p[x] != x: |
| 75 | + self.p[x] = self.find(self.p[x]) |
| 76 | + return self.p[x] |
| 77 | + |
55 | 78 |
|
| 79 | +class Solution: |
| 80 | + def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]: |
| 81 | + n = len(edges) |
| 82 | + p = list(range(n + 1)) |
| 83 | + uf = UnionFind(n + 1) |
| 84 | + conflict = cycle = None |
| 85 | + for i, (u, v) in enumerate(edges): |
| 86 | + if p[v] != v: |
| 87 | + conflict = i |
| 88 | + else: |
| 89 | + p[v] = u |
| 90 | + if not uf.union(u, v): |
| 91 | + cycle = i |
| 92 | + if conflict is None: |
| 93 | + return edges[cycle] |
| 94 | + v = edges[conflict][1] |
| 95 | + if cycle is not None: |
| 96 | + return [p[v], v] |
| 97 | + return edges[conflict] |
56 | 98 | ```
|
57 | 99 |
|
58 | 100 | ### **Java**
|
59 | 101 |
|
60 | 102 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
61 | 103 |
|
62 | 104 | ```java
|
| 105 | +class Solution { |
| 106 | + public int[] findRedundantDirectedConnection(int[][] edges) { |
| 107 | + int n = edges.length; |
| 108 | + int[] p = new int[n + 1]; |
| 109 | + for (int i = 0; i <= n; ++i) { |
| 110 | + p[i] = i; |
| 111 | + } |
| 112 | + UnionFind uf = new UnionFind(n + 1); |
| 113 | + int conflict = -1, cycle = -1; |
| 114 | + for (int i = 0; i < n; ++i) { |
| 115 | + int u = edges[i][0], v = edges[i][1]; |
| 116 | + if (p[v] != v) { |
| 117 | + conflict = i; |
| 118 | + } else { |
| 119 | + p[v] = u; |
| 120 | + if (!uf.union(u, v)) { |
| 121 | + cycle = i; |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + if (conflict == -1) { |
| 126 | + return edges[cycle]; |
| 127 | + } |
| 128 | + int v = edges[conflict][1]; |
| 129 | + if (cycle != -1) { |
| 130 | + return new int[]{p[v], v}; |
| 131 | + } |
| 132 | + return edges[conflict]; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +class UnionFind { |
| 137 | + public int[] p; |
| 138 | + public int n; |
| 139 | + |
| 140 | + public UnionFind(int n) { |
| 141 | + p = new int[n]; |
| 142 | + for (int i = 0; i < n; ++i) { |
| 143 | + p[i] = i; |
| 144 | + } |
| 145 | + this.n = n; |
| 146 | + } |
| 147 | + |
| 148 | + public boolean union(int a, int b) { |
| 149 | + int pa = find(a); |
| 150 | + int pb = find(b); |
| 151 | + if (pa == pb) { |
| 152 | + return false; |
| 153 | + } |
| 154 | + p[pa] = pb; |
| 155 | + --n; |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
| 159 | + public int find(int x) { |
| 160 | + if (p[x] != x) { |
| 161 | + p[x] = find(p[x]); |
| 162 | + } |
| 163 | + return p[x]; |
| 164 | + } |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +### **C++** |
| 169 | + |
| 170 | +```cpp |
| 171 | +class UnionFind { |
| 172 | +public: |
| 173 | + vector<int> p; |
| 174 | + int n; |
| 175 | + |
| 176 | + UnionFind(int _n): n(_n), p(_n) { |
| 177 | + iota(p.begin(), p.end(), 0); |
| 178 | + } |
| 179 | + |
| 180 | + bool unite(int a, int b) { |
| 181 | + int pa = find(a), pb = find(b); |
| 182 | + if (pa == pb) return false; |
| 183 | + p[pa] = pb; |
| 184 | + --n; |
| 185 | + return true; |
| 186 | + } |
| 187 | + |
| 188 | + int find(int x) { |
| 189 | + if (p[x] != x) p[x] = find(p[x]); |
| 190 | + return p[x]; |
| 191 | + } |
| 192 | +}; |
| 193 | + |
| 194 | +class Solution { |
| 195 | +public: |
| 196 | + vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) { |
| 197 | + int n = edges.size(); |
| 198 | + vector<int> p(n + 1); |
| 199 | + for (int i = 0; i <= n; ++i) p[i] = i; |
| 200 | + UnionFind uf(n + 1); |
| 201 | + int conflict = -1, cycle = -1; |
| 202 | + for (int i = 0; i < n; ++i) |
| 203 | + { |
| 204 | + int u = edges[i][0], v = edges[i][1]; |
| 205 | + if (p[v] != v) conflict = i; |
| 206 | + else |
| 207 | + { |
| 208 | + p[v] = u; |
| 209 | + if (!uf.unite(u, v)) cycle = i; |
| 210 | + } |
| 211 | + } |
| 212 | + if (conflict == -1) return edges[cycle]; |
| 213 | + int v = edges[conflict][1]; |
| 214 | + if (cycle != -1) return {p[v], v}; |
| 215 | + return edges[conflict]; |
| 216 | + } |
| 217 | +}; |
| 218 | +``` |
| 219 | +
|
| 220 | +### **Go** |
| 221 | +
|
| 222 | +```go |
| 223 | +type unionFind struct { |
| 224 | + p []int |
| 225 | + n int |
| 226 | +} |
| 227 | +
|
| 228 | +func newUnionFind(n int) *unionFind { |
| 229 | + p := make([]int, n) |
| 230 | + for i := range p { |
| 231 | + p[i] = i |
| 232 | + } |
| 233 | + return &unionFind{p, n} |
| 234 | +} |
| 235 | +
|
| 236 | +func (uf *unionFind) find(x int) int { |
| 237 | + if uf.p[x] != x { |
| 238 | + uf.p[x] = uf.find(uf.p[x]) |
| 239 | + } |
| 240 | + return uf.p[x] |
| 241 | +} |
| 242 | +
|
| 243 | +func (uf *unionFind) union(a, b int) bool { |
| 244 | + if uf.find(a) == uf.find(b) { |
| 245 | + return false |
| 246 | + } |
| 247 | + uf.p[uf.find(a)] = uf.find(b) |
| 248 | + uf.n-- |
| 249 | + return true |
| 250 | +} |
63 | 251 |
|
| 252 | +func findRedundantDirectedConnection(edges [][]int) []int { |
| 253 | + n := len(edges) |
| 254 | + p := make([]int, n+1) |
| 255 | + for i := range p { |
| 256 | + p[i] = i |
| 257 | + } |
| 258 | + uf := newUnionFind(n + 1) |
| 259 | + conflict, cycle := -1, -1 |
| 260 | + for i, e := range edges { |
| 261 | + u, v := e[0], e[1] |
| 262 | + if p[v] != v { |
| 263 | + conflict = i |
| 264 | + } else { |
| 265 | + p[v] = u |
| 266 | + if !uf.union(u, v) { |
| 267 | + cycle = i |
| 268 | + } |
| 269 | + } |
| 270 | + } |
| 271 | + if conflict == -1 { |
| 272 | + return edges[cycle] |
| 273 | + } |
| 274 | + v := edges[conflict][1] |
| 275 | + if cycle != -1 { |
| 276 | + return []int{p[v], v} |
| 277 | + } |
| 278 | + return edges[conflict] |
| 279 | +} |
64 | 280 | ```
|
65 | 281 |
|
66 | 282 | ### **...**
|
|
0 commit comments