{"id":7534,"date":"2020-10-18T12:40:14","date_gmt":"2020-10-18T19:40:14","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7534"},"modified":"2020-10-18T12:45:35","modified_gmt":"2020-10-18T19:45:35","slug":"leetcode-1627-graph-connectivity-with-threshold","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1627-graph-connectivity-with-threshold\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1627. Graph Connectivity With Threshold"},"content":{"rendered":"\n<p>We have&nbsp;<code>n<\/code>&nbsp;cities labeled from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>. Two different cities with labels&nbsp;<code>x<\/code>&nbsp;and&nbsp;<code>y<\/code>&nbsp;are directly connected by a bidirectional road if and only if&nbsp;<code>x<\/code>&nbsp;and&nbsp;<code>y<\/code>&nbsp;share a common divisor&nbsp;<strong>strictly greater<\/strong>&nbsp;than some&nbsp;<code>threshold<\/code>. More formally, cities with labels&nbsp;<code>x<\/code>&nbsp;and&nbsp;<code>y<\/code>&nbsp;have a road between them if there exists an integer&nbsp;<code>z<\/code>&nbsp;such that all of the following are true:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>x % z == 0<\/code>,<\/li><li><code>y % z == 0<\/code>, and<\/li><li><code>z &gt; threshold<\/code>.<\/li><\/ul>\n\n\n\n<p>Given the two integers,&nbsp;<code>n<\/code>&nbsp;and&nbsp;<code>threshold<\/code>, and an array of&nbsp;<code>queries<\/code>, you must determine for each&nbsp;<code>queries[i] = [a<sub>i<\/sub>, b<sub>i<\/sub>]<\/code>&nbsp;if cities&nbsp;<code>a<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>b<sub>i<\/sub><\/code>&nbsp;are connected (i.e. there is some path between them).<\/p>\n\n\n\n<p>Return&nbsp;<em>an array&nbsp;<\/em><code>answer<\/code><em>, where&nbsp;<\/em><code>answer.length == queries.length<\/code><em>&nbsp;and&nbsp;<\/em><code>answer[i]<\/code><em>&nbsp;is&nbsp;<\/em><code>true<\/code><em>&nbsp;if for the&nbsp;<\/em><code>i<sup>th<\/sup><\/code><em>&nbsp;query, there is a path between&nbsp;<\/em><code>a<sub>i<\/sub><\/code><em>&nbsp;and&nbsp;<\/em><code>b<sub>i<\/sub><\/code><em>, or&nbsp;<\/em><code>answer[i]<\/code><em>&nbsp;is&nbsp;<\/em><code>false<\/code><em>&nbsp;if there is no path.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/09\/ex1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 6, threshold = 2, queries = [[1,4],[2,5],[3,6]]\n<strong>Output:<\/strong> [false,false,true]\n<strong>Explanation:<\/strong> The divisors for each number:\n1:   1\n2:   1, 2\n3:   1, 3\n4:   1, 2, 4\n5:   1, 5\n6:   1, 2, 3, 6\nUsing the underlined divisors above the threshold, only cities 3 and 6 share a common divisor, so they are the\nonly ones directly connected. The result of each query:\n[1,4]   1 is not connected to 4\n[2,5]   2 is not connected to 5\n[3,6]   3 is connected to 6 through path 3--6\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/10\/tmp.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 6, threshold = 0, queries = [[4,5],[3,4],[3,2],[2,6],[1,3]]\n<strong>Output:<\/strong> [true,true,true,true,true]\n<strong>Explanation:<\/strong> The divisors for each number are the same as the previous example. However, since the threshold is 0,\nall divisors can be used. Since all numbers share 1 as a divisor, all cities are connected.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/10\/17\/ex3.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, threshold = 1, queries = [[4,5],[4,5],[3,2],[2,3],[3,4]]\n<strong>Output:<\/strong> [false,false,false,false,false]\n<strong>Explanation:<\/strong> 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.\nPlease 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].\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= n &lt;= 10<sup>4<\/sup><\/code><\/li><li><code>0 &lt;= threshold &lt;= n<\/code><\/li><li><code>1 &lt;= queries.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>queries[i].length == 2<\/code><\/li><li><code>1 &lt;= a<sub>i<\/sub>, b<sub>i<\/sub>&nbsp;&lt;= cities<\/code><\/li><li><code>a<sub>i<\/sub>&nbsp;!= b<sub>i<\/sub><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Union Find<\/strong><\/h2>\n\n\n\n<p>For x, merge 2x, 3x, 4x, ..,<br>If a number is already &#8220;merged&#8221;, skip it.<\/p>\n\n\n\n<p>Time complexity: O(nlogn? + queries)? <br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<bool> areConnected(int n, int threshold, vector<vector<int>>& queries) {\n    if (threshold == 0) return vector<bool>(queries.size(), true);\n    \n    vector<int> ds(n + 1);\n    iota(begin(ds), end(ds), 0);\n    function<int(int)> find = [&](int x) {\n      return ds[x] == x ? x : ds[x] = find(ds[x]);\n    };\n    \n    for (int x = threshold + 1; x <= n; ++x)\n      if (ds[x] == x)\n        for (int y = 2 * x; y <= n; y += x)    \n          ds[max(find(x), find(y))] = min(find(x), find(y));\n    \n    vector<bool> ans;\n    for (const vector<int>& q : queries)\n      ans.push_back(find(q[0]) == find(q[1]));    \n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def areConnected(self, n: int, threshold: int, \n                   queries: List[List[int]]) -> List[bool]:\n    if threshold == 0: return [True] * len(queries)\n    \n    ds = list(range(n + 1))\n    def find(x: int) -> int:\n      if x != ds[x]: ds[x] = find(ds[x])\n      return ds[x]\n\n    for x in range(threshold + 1, n + 1):\n      if ds[x] == x:\n        for y in range(2 * x, n + 1, x):\n          ds[max(find(x), find(y))] = min(find(x), find(y))\n\n    return [find(x) == find(y) for x, y in queries]\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We have&nbsp;n&nbsp;cities labeled from&nbsp;1&nbsp;to&nbsp;n. Two different cities with labels&nbsp;x&nbsp;and&nbsp;y&nbsp;are directly connected by a bidirectional road if and only if&nbsp;x&nbsp;and&nbsp;y&nbsp;share a common divisor&nbsp;strictly greater&nbsp;than some&nbsp;threshold. More&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[441,77,217,113],"class_list":["post-7534","post","type-post","status-publish","format-standard","hentry","category-graph","tag-factor","tag-graph","tag-hard","tag-union-find","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7534"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7534\/revisions"}],"predecessor-version":[{"id":7537,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7534\/revisions\/7537"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}