Graph Connectivity With Threshold Problem
Description
LeetCode Problem 1627.
We have n cities labeled from 1 to n. Two different cities with labels x and y are directly connected by a bidirectional road if and only if x and y share a common divisor strictly greater than some threshold. More formally, cities with labels x and y have a road between them if there exists an integer z such that all of the following are true:
x % z == 0,y % z == 0, andz > threshold.
Given the two integers, n and threshold, and an array of queries, you must determine for each queries[i] = [a_i, b_i] if cities a_i and b_i are connected directly or indirectly. (i.e. there is some path between them).
Return an array answer, where answer.length == queries.length and answer[i] is true if for the i^th query, there is a path between a_i and b_i, or answer[i] is false if there is no path.
Example 1:

Input: n = 6, threshold = 2, queries = [[1,4],[2,5],[3,6]]
Output: [false,false,true]
Explanation: The divisors for each number:
1: 1
2: 1, 2
3: 1, 3
4: 1, 2, 4
5: 1, 5
6: 1, 2, 3, 6
Using the underlined divisors above the threshold, only cities 3 and 6 share a common divisor, so they are the
only ones directly connected. The result of each query:
[1,4] 1 is not connected to 4
[2,5] 2 is not connected to 5
[3,6] 3 is connected to 6 through path 3--6
Example 2:

Input: n = 6, threshold = 0, queries = [[4,5],[3,4],[3,2],[2,6],[1,3]]
Output: [true,true,true,true,true]
Explanation: The divisors for each number are the same as the previous example. However, since the threshold is 0,
all divisors can be used. Since all numbers share 1 as a divisor, all cities are connected.
Example 3:

Input: n = 5, threshold = 1, queries = [[4,5],[4,5],[3,2],[2,3],[3,4]]
Output: [false,false,false,false,false]
Explanation: Only cities 2 and 4 share a common divisor 2 which is strictly greater than the threshold 1, so they are the only ones directly connected.
Please notice that there can be multiple queries for the same pair of nodes [x, y], and that the query [x, y] is equivalent to the query [y, x].
Constraints:
2 <= n <= 10^40 <= threshold <= n1 <= queries.length <= 10^5queries[i].length == 21 <= a_i, b_i <= citiesa_i != b_i
Solution
Approach: Union-Find Over Multiples
Testing every pair of cities for a common divisor above threshold would take O(n2) gcd computations. Look at it from the divisor's side instead: for each z > threshold, all multiples of z (z, 2z, 3z, ...) share the divisor z, so they are all directly connected to each other.
For connectivity it is enough to join each multiple with z itself, which links the whole group. Use a union-find (disjoint set) structure:
- For every
zfromthreshold + 1ton, unionzwith2z, 3z, ...up ton. - For a query
[a, b], the answer is whetherfind(a) == find(b).
Every road in the graph comes from some common divisor z > threshold, and both of its cities are multiples of that z, so the unions cover every road and nothing else. When threshold = 0, z = 1 joins all cities; when threshold >= n / 2, no z has a second multiple and every city stays alone.
The loops run n/(t+1) + n/(t+2) + ... + n/n times in total, which is at most n times the harmonic sum, so about n log n unions.
Complexity: O(n log n · α(n) + q · α(n)) time for q queries, where α is the nearly constant inverse Ackermann function, and O(n) extra space.
class Solution:
def areConnected(self, n: int, threshold: int, queries: List[List[int]]) -> List[bool]:
parent = list(range(n + 1))
def find(x: int) -> int:
while parent[x] != x:
parent[x] = parent[parent[x]] # path halving
x = parent[x]
return x
# Every z > threshold joins all of its multiples z, 2z, 3z, ... into one component.
for z in range(threshold + 1, n + 1):
root = find(z)
for multiple in range(2 * z, n + 1, z):
other = find(multiple)
if other != root:
parent[other] = root
return [find(a) == find(b) for a, b in queries]