Skip to Content
LeetCode The Hard Way
2000 - 2099

2076 - Process Restricted Friend Requests (Hard)

https://leetcode.com/problems/process-restricted-friend-requests

Problem Statement

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 cannotbecome 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 successfulif uj and vj 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, uj and vj become direct friends for all future friend requests.

Return a boolean arrayresult,where eachresult[j]istrueif thejthfriend request is successful orfalseif it is not.

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

Example 1:

Input: n = 3, restrictions = [[0,1]], requests = [[0,2],[2,1]]
Output: [true,false]
Explanation:
Request 0: Person 0 and person 2 can be friends, so they become direct friends. 
Request 1: Person 2 and person 1 cannot be friends since person 0 and person 1 would be indirect friends (1--2--0).

Example 2:

Input: n = 3, restrictions = [[0,1]], requests = [[1,2],[0,2]]
Output: [true,false]
Explanation:
Request 0: Person 1 and person 2 can be friends, so they become direct friends.
Request 1: Person 0 and person 2 cannot be friends since person 0 and person 1 would be indirect friends (0--2--1).

Example 3:

Input: n = 5, restrictions = [[0,1],[1,2],[2,3]], requests = [[0,4],[1,2],[3,1],[3,4]]
Output: [true,false,true,false]
Explanation:
Request 0: Person 0 and person 4 can be friends, so they become direct friends.
Request 1: Person 1 and person 2 cannot be friends since they are directly restricted.
Request 2: Person 3 and person 1 can be friends, so they become direct friends.
Request 3: Person 3 and person 4 cannot be friends since person 0 and person 1 would be indirect friends (0--4--3--1).

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

Approach 1: DSU

We need to process each friend request one at a time. When two people become friends, their entire friend groups become connected. Therefore, instead of storing every friendship separately, we keep track of groups of connected people.

The pp list stores which group each person belongs to. At the beginning, nobody is connected, so every person starts in their own group. This is why pp is initialized as [0,1,2,...,n−1][0, 1, 2, ..., n - 1]. f(x)f(x) finds the leader of the group containing person xx. Two people are in the same friend group when they have the same leader. The function also updates the parent values along the way, making future searches faster.

For each request [u,v][u, v], we find the leaders of their groups and store them in aa and bb. Accepting the request would connect everyone in group aa with everyone in group bb. Before accepting it, we examine every restricted pair [x,y][x, y]. We find the groups containing xx and yy. If one restricted person is in group aa and the other is in group bb, joining the groups would connect two people who are not allowed to become friends. In that case, the request must be rejected.

The set comparison f(x),f(y)!=a,b{f(x), f(y)} != {a, b} performs this check. If the two sets are equal, the restricted pair is split across the two groups we are trying to join, so the request is not allowed. If this never happens for any restriction, the request is safe. When a request is safe, we join the groups with p[a]=bp[a] = b. This makes group aa part of group bb. We then add TrueTrue to resres. If the request is not safe, we leave the groups unchanged and add FalseFalse. If the two people are already in the same group, accepting the request does not connect any new people.

With rr restrictions and qq requests, the time complexity is O(q∗r)O(q * r) and the space complexity is O(n)O(n).

Written by@wkw
class Solution:
    def friendRequests(self, n: int, restrictions: list[list[int]], requests: list[list[int]]) -> list[bool]:
        res = []
        p = list(range(n))
        def f(x):
            if p[x] != x: p[x] = f(p[x])
            return p[x]
        for u, v in requests:
            a, b = f(u), f(v)
            ok = all({f(x), f(y)} != {a, b} for x, y in restrictions)
            res.append(ok)
            if ok: p[a] = b
        return res

On this page