|
84 | 84 |
|
85 | 85 | <!-- 这里可写通用的实现逻辑 -->
|
86 | 86 |
|
| 87 | +**方法一:并查集** |
| 88 | + |
| 89 | +我们可以枚举 $z$ 以及 $z$ 的倍数,用并查集将它们连通起来。这样,对于每个查询 $[a, b]$,我们只需要判断 $a$ 和 $b$ 是否在同一个连通块中即可。 |
| 90 | + |
| 91 | +时间复杂度 $O(n \times \log n \time (\alpha(n) + q))$,空间复杂度 $O(n)$。其中 $n$ 和 $q$ 分别是节点数和查询数,而 $\alpha$ 是阿克曼函数的反函数。 |
| 92 | + |
87 | 93 | <!-- tabs:start -->
|
88 | 94 |
|
89 | 95 | ### **Python3**
|
90 | 96 |
|
91 | 97 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
92 | 98 |
|
93 | 99 | ```python
|
94 |
| - |
| 100 | +class UnionFind: |
| 101 | + def __init__(self, n): |
| 102 | + self.p = list(range(n)) |
| 103 | + self.size = [1] * n |
| 104 | + |
| 105 | + def find(self, x): |
| 106 | + if self.p[x] != x: |
| 107 | + self.p[x] = self.find(self.p[x]) |
| 108 | + return self.p[x] |
| 109 | + |
| 110 | + def union(self, a, b): |
| 111 | + pa, pb = self.find(a), self.find(b) |
| 112 | + if pa == pb: |
| 113 | + return False |
| 114 | + if self.size[pa] > self.size[pb]: |
| 115 | + self.p[pb] = pa |
| 116 | + self.size[pa] += self.size[pb] |
| 117 | + else: |
| 118 | + self.p[pa] = pb |
| 119 | + self.size[pb] += self.size[pa] |
| 120 | + return True |
| 121 | + |
| 122 | + |
| 123 | +class Solution: |
| 124 | + def areConnected( |
| 125 | + self, n: int, threshold: int, queries: List[List[int]] |
| 126 | + ) -> List[bool]: |
| 127 | + uf = UnionFind(n + 1) |
| 128 | + for a in range(threshold + 1, n + 1): |
| 129 | + for b in range(a + a, n + 1, a): |
| 130 | + uf.union(a, b) |
| 131 | + return [uf.find(a) == uf.find(b) for a, b in queries] |
95 | 132 | ```
|
96 | 133 |
|
97 | 134 | ### **Java**
|
98 | 135 |
|
99 | 136 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
100 | 137 |
|
101 | 138 | ```java
|
| 139 | +class UnionFind { |
| 140 | + private int[] p; |
| 141 | + private int[] size; |
| 142 | + |
| 143 | + public UnionFind(int n) { |
| 144 | + p = new int[n]; |
| 145 | + size = new int[n]; |
| 146 | + for (int i = 0; i < n; ++i) { |
| 147 | + p[i] = i; |
| 148 | + size[i] = 1; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + public int find(int x) { |
| 153 | + if (p[x] != x) { |
| 154 | + p[x] = find(p[x]); |
| 155 | + } |
| 156 | + return p[x]; |
| 157 | + } |
| 158 | + |
| 159 | + public boolean union(int a, int b) { |
| 160 | + int pa = find(a), pb = find(b); |
| 161 | + if (pa == pb) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + if (size[pa] > size[pb]) { |
| 165 | + p[pb] = pa; |
| 166 | + size[pa] += size[pb]; |
| 167 | + } else { |
| 168 | + p[pa] = pb; |
| 169 | + size[pb] += size[pa]; |
| 170 | + } |
| 171 | + return true; |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +class Solution { |
| 176 | + public List<Boolean> areConnected(int n, int threshold, int[][] queries) { |
| 177 | + UnionFind uf = new UnionFind(n + 1); |
| 178 | + for (int a = threshold + 1; a <= n; ++a) { |
| 179 | + for (int b = a + a; b <= n; b += a) { |
| 180 | + uf.union(a, b); |
| 181 | + } |
| 182 | + } |
| 183 | + List<Boolean> ans = new ArrayList<>(); |
| 184 | + for (var q : queries) { |
| 185 | + ans.add(uf.find(q[0]) == uf.find(q[1])); |
| 186 | + } |
| 187 | + return ans; |
| 188 | + } |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +### **C++** |
| 193 | + |
| 194 | +```cpp |
| 195 | +class UnionFind { |
| 196 | +public: |
| 197 | + UnionFind(int n) { |
| 198 | + p = vector<int>(n); |
| 199 | + size = vector<int>(n, 1); |
| 200 | + iota(p.begin(), p.end(), 0); |
| 201 | + } |
| 202 | + |
| 203 | + bool unite(int a, int b) { |
| 204 | + int pa = find(a), pb = find(b); |
| 205 | + if (pa == pb) { |
| 206 | + return false; |
| 207 | + } |
| 208 | + if (size[pa] > size[pb]) { |
| 209 | + p[pb] = pa; |
| 210 | + size[pa] += size[pb]; |
| 211 | + } else { |
| 212 | + p[pa] = pb; |
| 213 | + size[pb] += size[pa]; |
| 214 | + } |
| 215 | + return true; |
| 216 | + } |
| 217 | + |
| 218 | + int find(int x) { |
| 219 | + if (p[x] != x) { |
| 220 | + p[x] = find(p[x]); |
| 221 | + } |
| 222 | + return p[x]; |
| 223 | + } |
| 224 | + |
| 225 | +private: |
| 226 | + vector<int> p, size; |
| 227 | +}; |
| 228 | + |
| 229 | +class Solution { |
| 230 | +public: |
| 231 | + vector<bool> areConnected(int n, int threshold, vector<vector<int>>& queries) { |
| 232 | + UnionFind uf(n + 1); |
| 233 | + for (int a = threshold + 1; a <= n; ++a) { |
| 234 | + for (int b = a + a; b <= n; b += a) { |
| 235 | + uf.unite(a, b); |
| 236 | + } |
| 237 | + } |
| 238 | + vector<bool> ans; |
| 239 | + for (auto& q : queries) { |
| 240 | + ans.push_back(uf.find(q[0]) == uf.find(q[1])); |
| 241 | + } |
| 242 | + return ans; |
| 243 | + } |
| 244 | +}; |
| 245 | +``` |
| 246 | +
|
| 247 | +### **Go** |
| 248 | +
|
| 249 | +```go |
| 250 | +type unionFind struct { |
| 251 | + p, size []int |
| 252 | +} |
| 253 | +
|
| 254 | +func newUnionFind(n int) *unionFind { |
| 255 | + p := make([]int, n) |
| 256 | + size := make([]int, n) |
| 257 | + for i := range p { |
| 258 | + p[i] = i |
| 259 | + size[i] = 1 |
| 260 | + } |
| 261 | + return &unionFind{p, size} |
| 262 | +} |
| 263 | +
|
| 264 | +func (uf *unionFind) find(x int) int { |
| 265 | + if uf.p[x] != x { |
| 266 | + uf.p[x] = uf.find(uf.p[x]) |
| 267 | + } |
| 268 | + return uf.p[x] |
| 269 | +} |
| 270 | +
|
| 271 | +func (uf *unionFind) union(a, b int) bool { |
| 272 | + pa, pb := uf.find(a), uf.find(b) |
| 273 | + if pa == pb { |
| 274 | + return false |
| 275 | + } |
| 276 | + if uf.size[pa] > uf.size[pb] { |
| 277 | + uf.p[pb] = pa |
| 278 | + uf.size[pa] += uf.size[pb] |
| 279 | + } else { |
| 280 | + uf.p[pa] = pb |
| 281 | + uf.size[pb] += uf.size[pa] |
| 282 | + } |
| 283 | + return true |
| 284 | +} |
| 285 | +
|
| 286 | +func areConnected(n int, threshold int, queries [][]int) []bool { |
| 287 | + uf := newUnionFind(n + 1) |
| 288 | + for a := threshold + 1; a <= n; a++ { |
| 289 | + for b := a + a; b <= n; b += a { |
| 290 | + uf.union(a, b) |
| 291 | + } |
| 292 | + } |
| 293 | + ans := make([]bool, len(queries)) |
| 294 | + for i, q := range queries { |
| 295 | + ans[i] = uf.find(q[0]) == uf.find(q[1]) |
| 296 | + } |
| 297 | + return ans |
| 298 | +} |
| 299 | +``` |
102 | 300 |
|
| 301 | +### **TypeScript** |
| 302 | + |
| 303 | +```ts |
| 304 | +class UnionFind { |
| 305 | + p: number[]; |
| 306 | + size: number[]; |
| 307 | + constructor(n: number) { |
| 308 | + this.p = Array(n) |
| 309 | + .fill(0) |
| 310 | + .map((_, i) => i); |
| 311 | + this.size = Array(n).fill(1); |
| 312 | + } |
| 313 | + |
| 314 | + find(x: number): number { |
| 315 | + if (this.p[x] !== x) { |
| 316 | + this.p[x] = this.find(this.p[x]); |
| 317 | + } |
| 318 | + return this.p[x]; |
| 319 | + } |
| 320 | + |
| 321 | + union(a: number, b: number): boolean { |
| 322 | + const [pa, pb] = [this.find(a), this.find(b)]; |
| 323 | + if (pa === pb) { |
| 324 | + return false; |
| 325 | + } |
| 326 | + if (this.size[pa] > this.size[pb]) { |
| 327 | + this.p[pb] = pa; |
| 328 | + this.size[pa] += this.size[pb]; |
| 329 | + } else { |
| 330 | + this.p[pa] = pb; |
| 331 | + this.size[pb] += this.size[pa]; |
| 332 | + } |
| 333 | + return true; |
| 334 | + } |
| 335 | +} |
| 336 | + |
| 337 | +function areConnected(n: number, threshold: number, queries: number[][]): boolean[] { |
| 338 | + const uf = new UnionFind(n + 1); |
| 339 | + for (let a = threshold + 1; a <= n; ++a) { |
| 340 | + for (let b = a * 2; b <= n; b += a) { |
| 341 | + uf.union(a, b); |
| 342 | + } |
| 343 | + } |
| 344 | + return queries.map(([a, b]) => uf.find(a) === uf.find(b)); |
| 345 | +} |
103 | 346 | ```
|
104 | 347 |
|
105 | 348 | ### **...**
|
|
0 commit comments