1627. Graph Connectivity With Threshold Hard

@problem@discussion
#Array#Math#Union Find



1/**
2 * [1627] Graph Connectivity With Threshold
3 *
4 * 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:
5 * 
6 * 	x % z == 0,
7 * 	y % z == 0, and
8 * 	z > threshold.
9 * 
10 * Given the two integers, n and threshold, and an array of queries, you must determine for each queries[i] = [ai, bi] if cities ai and bi are connected directly or indirectly. (i.e. there is some path between them).
11 * 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 ai and bi, or answer[i] is false if there is no path.
12 *  
13 * Example 1:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/09/ex1.jpg" style="width: 382px; height: 181px;" />
15 * Input: n = 6, threshold = 2, queries = [[1,4],[2,5],[3,6]]
16 * Output: [false,false,true]
17 * Explanation: The divisors for each number:
18 * 1:   1
19 * 2:   1, 2
20 * 3:   1, <u>3</u>
21 * 4:   1, 2, <u>4</u>
22 * 5:   1, <u>5</u>
23 * 6:   1, 2, <u>3</u>, <u>6</u>
24 * Using the underlined divisors above the threshold, only cities 3 and 6 share a common divisor, so they are the
25 * only ones directly connected. The result of each query:
26 * [1,4]   1 is not connected to 4
27 * [2,5]   2 is not connected to 5
28 * [3,6]   3 is connected to 6 through path 3--6
29 * 
30 * Example 2:
31 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/10/tmp.jpg" style="width: 532px; height: 302px;" />
32 * Input: n = 6, threshold = 0, queries = [[4,5],[3,4],[3,2],[2,6],[1,3]]
33 * Output: [true,true,true,true,true]
34 * Explanation: The divisors for each number are the same as the previous example. However, since the threshold is 0,
35 * all divisors can be used. Since all numbers share 1 as a divisor, all cities are connected.
36 * 
37 * Example 3:
38 * <img alt="" src="https://assets.leetcode.com/uploads/2020/10/17/ex3.jpg" style="width: 282px; height: 282px;" />
39 * Input: n = 5, threshold = 1, queries = [[4,5],[4,5],[3,2],[2,3],[3,4]]
40 * Output: [false,false,false,false,false]
41 * 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.
42 * 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].
43 * 
44 *  
45 * Constraints:
46 * 
47 * 	2 <= n <= 10^4
48 * 	0 <= threshold <= n
49 * 	1 <= queries.length <= 10^5
50 * 	queries[i].length == 2
51 * 	1 <= ai, bi <= cities
52 * 	ai != bi
53 * 
54 */
55pub struct Solution {}
56
57// problem: https://leetcode.com/problems/graph-connectivity-with-threshold/
58// discuss: https://leetcode.com/problems/graph-connectivity-with-threshold/discuss/?currentPage=1&orderBy=most_votes&query=
59
60// submission codes start here
61
62impl Solution {
63    pub fn are_connected(n: i32, threshold: i32, queries: Vec<Vec<i32>>) -> Vec<bool> {
64        vec![]
65    }
66}
67
68// submission codes end
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73
74    #[test]
75    fn test_1627() {
76    }
77}
78


Back
© 2025 bowen.ge All Rights Reserved.