|
| 1 | +2076\. Process Restricted Friend Requests |
| 2 | + |
| 3 | +Hard |
| 4 | + |
| 5 | +You are given an integer `n` indicating the number of people in a network. Each person is labeled from `0` to `n - 1`. |
| 6 | + |
| 7 | +You are also given a **0-indexed** 2D integer array `restrictions`, where <code>restrictions[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> means that person <code>x<sub>i</sub></code> and person <code>y<sub>i</sub></code> **cannot** become **friends**, either **directly** or **indirectly** through other people. |
| 8 | + |
| 9 | +Initially, no one is friends with each other. You are given a list of friend requests as a **0-indexed** 2D integer array `requests`, where <code>requests[j] = [u<sub>j</sub>, v<sub>j</sub>]</code> is a friend request between person <code>u<sub>j</sub></code> and person <code>v<sub>j</sub></code>. |
| 10 | + |
| 11 | +A friend request is **successful** if <code>u<sub>j</sub></code> and <code>v<sub>j</sub></code> can be **friends**. Each friend request is processed in the given order (i.e., `requests[j]` occurs before `requests[j + 1]`), and upon a successful request, <code>u<sub>j</sub></code> and <code>v<sub>j</sub></code> **become direct friends** for all future friend requests. |
| 12 | + |
| 13 | +Return _a **boolean array**_ `result`, _where each_ `result[j]` _is_ `true` _if the_ <code>j<sup>th</sup></code> _friend request is **successful** or_ `false` _if it is not_. |
| 14 | + |
| 15 | +**Note:** If <code>u<sub>j</sub></code> and <code>v<sub>j</sub></code> are already direct friends, the request is still **successful**. |
| 16 | + |
| 17 | +**Example 1:** |
| 18 | + |
| 19 | +**Input:** n = 3, restrictions = [[0,1]], requests = [[0,2],[2,1]] |
| 20 | + |
| 21 | +**Output:** [true,false] |
| 22 | + |
| 23 | +**Explanation:** |
| 24 | + |
| 25 | +Request 0: Person 0 and person 2 can be friends, so they become direct friends. |
| 26 | + |
| 27 | +Request 1: Person 2 and person 1 cannot be friends since person 0 and person 1 would be indirect friends (1--2--0). |
| 28 | + |
| 29 | +**Example 2:** |
| 30 | + |
| 31 | +**Input:** n = 3, restrictions = [[0,1]], requests = [[1,2],[0,2]] |
| 32 | + |
| 33 | +**Output:** [true,false] |
| 34 | + |
| 35 | +**Explanation:** |
| 36 | + |
| 37 | +Request 0: Person 1 and person 2 can be friends, so they become direct friends. |
| 38 | + |
| 39 | +Request 1: Person 0 and person 2 cannot be friends since person 0 and person 1 would be indirect friends (0--2--1). |
| 40 | + |
| 41 | +**Example 3:** |
| 42 | + |
| 43 | +**Input:** n = 5, restrictions = [[0,1],[1,2],[2,3]], requests = [[0,4],[1,2],[3,1],[3,4]] |
| 44 | + |
| 45 | +**Output:** [true,false,true,false] |
| 46 | + |
| 47 | +**Explanation:** |
| 48 | + |
| 49 | +Request 0: Person 0 and person 4 can be friends, so they become direct friends. |
| 50 | + |
| 51 | +Request 1: Person 1 and person 2 cannot be friends since they are directly restricted. |
| 52 | + |
| 53 | +Request 2: Person 3 and person 1 can be friends, so they become direct friends. |
| 54 | + |
| 55 | +Request 3: Person 3 and person 4 cannot be friends since person 0 and person 1 would be indirect friends (0--4--3--1). |
| 56 | + |
| 57 | +**Constraints:** |
| 58 | + |
| 59 | +* `2 <= n <= 1000` |
| 60 | +* `0 <= restrictions.length <= 1000` |
| 61 | +* `restrictions[i].length == 2` |
| 62 | +* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= n - 1</code> |
| 63 | +* <code>x<sub>i</sub> != y<sub>i</sub></code> |
| 64 | +* `1 <= requests.length <= 1000` |
| 65 | +* `requests[j].length == 2` |
| 66 | +* <code>0 <= u<sub>j</sub>, v<sub>j</sub> <= n - 1</code> |
| 67 | +* <code>u<sub>j</sub> != v<sub>j</sub></code> |
0 commit comments