Process Restricted Friend Requests

You are given an integer n indicating the number of people in a network. Each person is labeled from 0 to n - 1.

You are also given a 0-indexed 2D integer array restrictions, where restrictions[i] = [xi, yi] means that person xi and person yi cannot become friends, either directly or indirectly through other people.

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 requests[j] = [uj, vj] is a friend request between person uj and person vj.

A friend request is successful if uj and vj can be friends. Each friend request is processed in the given order, meaning requests[j] occurs before requests[j + 1], and upon a successful request, uj and vj become direct friends for all future friend requests.

Return a boolean array result, where each result[j] is true if the j^th friend request is successful or false if it is not.

Note: If uj and vj are already direct friends, the request is still successful.

Example 1
Inputn = 3, restrictions = [[0,1]], requests = [[0,2],[2,1]]
Output[true,false]
Request 0 succeeds because person 0 and person 2 can be friends, but request 1 fails because it would make person 0 and person 1 indirect friends.
Example 2
Inputn = 3, restrictions = [[0,1]], requests = [[1,2],[0,2]]
Output[true,false]
Request 0 succeeds because person 1 and person 2 can be friends, but request 1 fails because it would make person 0 and person 1 indirect friends.

Constraints

  • 2 <= n <= 1000
  • 0 <= restrictions.length <= 1000
  • restrictions[i].length == 2
  • 0 <= xi, yi <= n - 1
  • xi != yi
  • 1 <= requests.length <= 1000
  • requests[j].length == 2
  • 0 <= uj, vj <= n - 1
  • uj != vj

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate