{"id":8719,"date":"2021-11-15T13:04:13","date_gmt":"2021-11-15T21:04:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8719"},"modified":"2021-11-15T13:05:03","modified_gmt":"2021-11-15T21:05:03","slug":"leetcode-2076-process-restricted-friend-requests","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-2076-process-restricted-friend-requests\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2076. Process Restricted Friend Requests"},"content":{"rendered":"\n<p>You are given an integer&nbsp;<code>n<\/code>&nbsp;indicating the number of people in a network. Each person is labeled from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>.<\/p>\n\n\n\n<p>You are also given a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D integer array&nbsp;<code>restrictions<\/code>, where&nbsp;<code>restrictions[i] = [x<sub>i<\/sub>, y<sub>i<\/sub>]<\/code>&nbsp;means that person&nbsp;<code>x<sub>i<\/sub><\/code>&nbsp;and person&nbsp;<code>y<sub>i<\/sub><\/code>&nbsp;<strong>cannot&nbsp;<\/strong>become&nbsp;<strong>friends<\/strong>,either&nbsp;<strong>directly<\/strong>&nbsp;or&nbsp;<strong>indirectly<\/strong>&nbsp;through other people.<\/p>\n\n\n\n<p>Initially, no one is friends with each other. You are given a list of friend requests as a&nbsp;<strong>0-indexed<\/strong>&nbsp;2D integer array&nbsp;<code>requests<\/code>, where&nbsp;<code>requests[j] = [u<sub>j<\/sub>, v<sub>j<\/sub>]<\/code>&nbsp;is a friend request between person&nbsp;<code>u<sub>j<\/sub><\/code>&nbsp;and person&nbsp;<code>v<sub>j<\/sub><\/code>.<\/p>\n\n\n\n<p>A friend request is&nbsp;<strong>successful&nbsp;<\/strong>if&nbsp;<code>u<sub>j<\/sub><\/code>&nbsp;and&nbsp;<code>v<sub>j<\/sub><\/code>&nbsp;can be&nbsp;<strong>friends<\/strong>. Each friend request is processed in the given order (i.e.,&nbsp;<code>requests[j]<\/code>&nbsp;occurs before&nbsp;<code>requests[j + 1]<\/code>), and upon a successful request,&nbsp;<code>u<sub>j<\/sub><\/code>&nbsp;and&nbsp;<code>v<sub>j<\/sub><\/code>&nbsp;<strong>become direct friends<\/strong>&nbsp;for all future friend requests.<\/p>\n\n\n\n<p>Return&nbsp;<em>a&nbsp;<strong>boolean array<\/strong>&nbsp;<\/em><code>result<\/code>,<em>&nbsp;where each&nbsp;<\/em><code>result[j]<\/code><em>&nbsp;is&nbsp;<\/em><code>true<\/code><em>&nbsp;if the&nbsp;<\/em><code>j<sup>th<\/sup><\/code><em>&nbsp;friend request is&nbsp;<strong>successful<\/strong>&nbsp;or&nbsp;<\/em><code>false<\/code><em>&nbsp;if it is not<\/em>.<\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;If&nbsp;<code>u<sub>j<\/sub><\/code>&nbsp;and&nbsp;<code>v<sub>j<\/sub><\/code>&nbsp;are already direct friends, the request is still&nbsp;<strong>successful<\/strong>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 3, restrictions = [[0,1]], requests = [[0,2],[2,1]]\n<strong>Output:<\/strong> [true,false]\n<strong>Explanation:\n<\/strong>Request 0: Person 0 and person 2 can be friends, so they become direct friends. \nRequest 1: Person 2 and person 1 cannot be friends since person 0 and person 1 would be indirect friends (1--2--0).\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 = 3, restrictions = [[0,1]], requests = [[1,2],[0,2]]\n<strong>Output:<\/strong> [true,false]\n<strong>Explanation:\n<\/strong>Request 0: Person 1 and person 2 can be friends, so they become direct friends.\nRequest 1: Person 0 and person 2 cannot be friends since person 0 and person 1 would be indirect friends (0--2--1).\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 5, restrictions = [[0,1],[1,2],[2,3]], requests = [[0,4],[1,2],[3,1],[3,4]]\n<strong>Output:<\/strong> [true,false,true,false]\n<strong>Explanation:\n<\/strong>Request 0: Person 0 and person 4 can be friends, so they become direct friends.\nRequest 1: Person 1 and person 2 cannot be friends since they are directly restricted.\nRequest 2: Person 3 and person 1 can be friends, so they become direct friends.\nRequest 3: Person 3 and person 4 cannot be friends since person 0 and person 1 would be indirect friends (0--4--3--1).\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;= 1000<\/code><\/li><li><code>0 &lt;= restrictions.length &lt;= 1000<\/code><\/li><li><code>restrictions[i].length == 2<\/code><\/li><li><code>0 &lt;= x<sub>i<\/sub>, y<sub>i<\/sub>&nbsp;&lt;= n - 1<\/code><\/li><li><code>x<sub>i<\/sub>&nbsp;!= y<sub>i<\/sub><\/code><\/li><li><code>1 &lt;= requests.length &lt;= 1000<\/code><\/li><li><code>requests[j].length == 2<\/code><\/li><li><code>0 &lt;= u<sub>j<\/sub>, v<sub>j<\/sub>&nbsp;&lt;= n - 1<\/code><\/li><li><code>u<sub>j<\/sub>&nbsp;!= v<sub>j<\/sub><\/code><\/li><\/ul>\n\n\n\n<p><strong>Solution: Union Find \/ Brute Force<\/strong><\/p>\n\n\n\n<p>For each request, check all restrictions.<\/p>\n\n\n\n<p>Time complexity: O(req * res)<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> friendRequests(int n, vector<vector<int>>& restrictions, vector<vector<int>>& requests) {\n    vector<int> parents(n);\n    iota(begin(parents), end(parents), 0);\n    function<int(int)> find = [&](int x) {\n      if (parents[x] == x) return x;\n      return parents[x] = find(parents[x]);\n    };\n    auto check = [&](int u, int v) {\n      for (const auto& r : restrictions) {\n        int pu = find(r[0]);\n        int pv = find(r[1]);\n        if ((pu == u && pv == v) || (pu == v && pv == u))\n          return false;\n      }\n      return true;\n    };\n    vector<bool> ans;\n    for (const auto& r : requests) {      \n      int pu = find(r[0]);\n      int pv = find(r[1]);\n      if (pu == pv || check(pu, pv)) {\n        parents[pu] = pv;\n        ans.push_back(true);\n      } else {\n        ans.push_back(false);\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer&nbsp;n&nbsp;indicating the number of people in a network. Each person is labeled from&nbsp;0&nbsp;to&nbsp;n &#8211; 1. You are also given a&nbsp;0-indexed&nbsp;2D integer&#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":[77,217,113],"class_list":["post-8719","post","type-post","status-publish","format-standard","hentry","category-graph","tag-graph","tag-hard","tag-union-find","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8719","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=8719"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8719\/revisions"}],"predecessor-version":[{"id":8721,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8719\/revisions\/8721"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}