{"id":8203,"date":"2021-03-06T10:30:54","date_gmt":"2021-03-06T18:30:54","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8203"},"modified":"2021-03-06T11:01:01","modified_gmt":"2021-03-06T19:01:01","slug":"leetcode-1782-count-pairs-of-nodes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1782-count-pairs-of-nodes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1782. Count Pairs Of Nodes"},"content":{"rendered":"\n<p>You are given an undirected graph represented by an integer&nbsp;<code>n<\/code>, which is the number of nodes, and&nbsp;<code>edges<\/code>, where&nbsp;<code>edges[i] = [u<sub>i<\/sub>, v<sub>i<\/sub>]<\/code>&nbsp;which indicates that there is an undirected edge between&nbsp;<code>u<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>v<sub>i<\/sub><\/code>. You are also given an integer array&nbsp;<code>queries<\/code>.<\/p>\n\n\n\n<p>The answer to the&nbsp;<code>j<sup>th<\/sup><\/code>&nbsp;query is the number of pairs of nodes&nbsp;<code>(a, b)<\/code>&nbsp;that satisfy the following conditions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>a &lt; b<\/code><\/li><li><code>cnt<\/code>&nbsp;is&nbsp;<strong>strictly greater<\/strong>&nbsp;than&nbsp;<code>queries[j]<\/code>, where&nbsp;<code>cnt<\/code>&nbsp;is the number of edges incident to&nbsp;<code>a<\/code>&nbsp;<strong>or<\/strong>&nbsp;<code>b<\/code>.<\/li><\/ul>\n\n\n\n<p>Return an array&nbsp;<code>answers<\/code>&nbsp;such that&nbsp;<code>answers.length == queries.length<\/code>&nbsp;and&nbsp;<code>answers[j]<\/code>&nbsp;is the answer of the&nbsp;<code>j<sup>th<\/sup><\/code>&nbsp;query.<\/p>\n\n\n\n<p>Note that there can be&nbsp;<strong>repeated edges<\/strong>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><img decoding=\"async\" alt=\"\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/02\/11\/screenshot-from-2021-02-11-23-07-35.png\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, edges = [[1,2],[2,4],[1,3],[2,3],[2,1]], queries = [2,3]\n<strong>Output:<\/strong> [6,5]\n<strong>Explanation:<\/strong> The number of edges incident to at least one of each pair is shown above.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, edges = [[1,5],[1,5],[3,4],[2,5],[1,3],[5,1],[2,3],[2,5]], queries = [1,2,3,4,5]\n<strong>Output:<\/strong> [10,10,9,8,6]\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;= 2 * 10<sup>4<\/sup><\/code><\/li><li><code>1 &lt;= edges.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= u<sub>i<\/sub>, v<sub>i<\/sub>&nbsp;&lt;= n<\/code><\/li><li><code>u<sub>i&nbsp;<\/sub>!= v<sub>i<\/sub><\/code><\/li><li><code>1 &lt;= queries.length &lt;= 20<\/code><\/li><li><code>0 &lt;= queries[j] &lt; edges.length<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Pre-compute <\/strong><\/h2>\n\n\n\n<p>Pre-compute # of pairs with total edges >= k. where k is from 0 to max_degree * 2 + 1.<\/p>\n\n\n\n<p>Time complexity: (|node_degrees|<sup>2<\/sup> + V + E)<br>Space complexity: O(V+E)<\/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<int> countPairs(int n, vector<vector<int>>& edges, vector<int>& queries) {\n    vector<int> node_degrees(n);\n    unordered_map<int, int> edge_freq;\n    for (auto& e : edges) {\n      sort(begin(e), end(e));\n      ++node_degrees[--e[0]];\n      ++node_degrees[--e[1]];\n      ++edge_freq[(e[0] << 16) | e[1]];\n    }\n    \n    const int max_degree = *max_element(begin(node_degrees), \n                                        end(node_degrees));\n    \/\/ Need pad one more to handle \"not found \/ 0\" case.\n    vector<int> counts(max_degree * 2 + 2);\n    \n    unordered_map<int, int> degree_count;\n    for (int i = 0; i < n; ++i) \n      ++degree_count[node_degrees[i]];\n    \n    for (auto&#038; [d1, c1] : degree_count)\n      for (auto&#038; [d2, c2] : degree_count)\n        \/\/ Only count once if degrees are different to ensure (a < b)\n        if (d1 < d2) counts[d1 + d2] += c1 * c2;\n        \/\/ If degrees are the same C(n, 2) to ensure (a < b)\n        else if (d1 == d2) counts[d1 * 2] += c1 * (c1 - 1) \/ 2;\n    \n    for (auto&#038; [key, freq] : edge_freq) {\n      const int u = key >> 16;\n      const int v = key & 0xFFFF;\n      \/\/ For a pair of (u, v) their actual edge count is \n      \/\/ d[u] + d[v] - freq[(u, v)] instead of d[u] + d[v]\n      counts[node_degrees[u] + node_degrees[v]] -= 1;\n      counts[node_degrees[u] + node_degrees[v] - freq] += 1;\n    }\n    \n    \/\/ counts[i] = # of pairs whose total edges >= i\n    for (int i = counts.size() - 2; i >= 0; --i)\n      counts[i] += counts[i + 1];\n    \n    vector<int> ans;\n    for (int q : queries)\n      ans.push_back(counts[min(q + 1, static_cast<int>(counts.size() - 1))]);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an undirected graph represented by an integer&nbsp;n, which is the number of nodes, and&nbsp;edges, where&nbsp;edges[i] = [ui, vi]&nbsp;which indicates that there is&#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":[413,77,217],"class_list":["post-8203","post","type-post","status-publish","format-standard","hentry","category-graph","tag-all-pairs","tag-graph","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8203","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=8203"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8203\/revisions"}],"predecessor-version":[{"id":8208,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8203\/revisions\/8208"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}