🔗 🔴 2076. Process Restricted Friend Requests

Problem Statement

題目簡述

有 nn 位編號為 00 到 n−1n - 1 的使用者,初始彼此皆非好友。給定限制清單 restrictions\textit{restrictions},其中 [xi,yi][x_i, y_i] 表示 xix_i 與 yiy_i 不能直接或間接成為好友。

依序處理好友請求清單 requests\textit{requests},其中 [uj,vj][u_j, v_j] 表示 uju_j 與 vjv_j 請求成為好友:若同意後不會違反任何限制,則請求成功且兩人成為直接好友;否則請求失敗且關係不變。回傳布林陣列表示每個請求是否成功。

Constraints

約束條件

  • 2≤n≤10002 \le n \le 1000
  • 0≤restrictions.length≤10000 \le \textit{restrictions.length} \le 1000,restrictions[i].length=2\textit{restrictions}[i].\text{length} = 2
  • 0≤xi,yi<n0 \le x_i, y_i < n,xi≠yix_i \ne y_i,所有限制對互不相同
  • 1≤requests.length≤10001 \le \textit{requests.length} \le 1000,requests[j].length=2\textit{requests}[j].\text{length} = 2
  • 0≤uj,vj<n0 \le u_j, v_j < n,uj≠vju_j \ne v_j,所有請求對互不相同

思路:併查集維護連通分量

好友關係具有傳遞性,「直接或間接成為好友」等價於兩位使用者位於同一個連通分量中。因此用併查集(Union-Find) 維護當前的連通分量(搭配路徑壓縮與按大小合併)。

處理請求 (x,y)(x, y) 時,先找到兩者的代表元素 fx=find(x)f_x = \text{find}(x)、fy=find(y)f_y = \text{find}(y):

  • 若 fx=fyf_x = f_y:兩人原本就在同一連通分量,請求直接成功,不會引入新的連通關係。
  • 若 fx≠fyf_x \ne f_y:一旦合併,唯一新增的連通對就是「分量 fxf_x 內的人」與「分量 fyf_y 內的人」之間的配對。因此合併前只需判斷這兩個分量之間是否橫跨任何一條限制;若無衝突才合併。

以下兩種做法的差別,在於如何檢查兩個分量之間是否存在衝突。

方法一:併查集 + 枚舉限制

由於請求數與限制數皆不超過 10001000,可以直接枚舉 restrictions\textit{restrictions} 中的每一對 (u,v)(u, v):

  • 查詢各自所屬的代表元素 find(u)\text{find}(u) 與 find(v)\text{find}(v)。
  • 若兩者恰好對應 {fx,fy}\{f_x, f_y\},表示合併 fxf_x 與 fyf_y 會讓 uu 與 vv 變成間接好友,該請求必須拒絕。
  • 若所有限制皆未觸發衝突,則將較小的分量併入較大的分量。

複雜度分析

  • 時間複雜度:O(n+q⋅m⋅α(n))\mathcal{O}(n + q \cdot m \cdot \alpha(n)),其中 m=∣restrictions∣m = |\textit{restrictions}|、q=∣requests∣q = |\textit{requests}|,α\alpha 為反阿克曼函數。
  • 空間複雜度:O(n)\mathcal{O}(n)(不計回傳陣列)。

方法二:併查集 + 位元遮罩(Bitmask)

觀察 n≤1000n \le 1000,可利用 Python 任意精度整數將每個節點集合壓成 nn 位元的位元遮罩,改以位元運算在 O(n/w)\mathcal{O}(n / w) 時間內完成衝突檢查與集合合併。

為每個連通分量的代表元素 rr 維護兩個位元集合:

  • comp[r]\textit{comp}[r]:分量 rr 內所有人的集合,初始 comp[i]=2i\textit{comp}[i] = 2^i(僅包含自己)。
  • ban[r]\textit{ban}[r]:與分量 rr 內至少一人存在限制關係的所有人的集合,初始將每條限制 (x,y)(x, y) 雙向加入 ban[x]\textit{ban}[x] 與 ban[y]\textit{ban}[y]。

檢查 fxf_x 與 fyf_y 能否合併時,只需判斷 ban[fx]&comp[fy]\textit{ban}[f_x] \mathbin{\&} \textit{comp}[f_y] 是否非零:若非零,表示分量 fyf_y 裡有人被分量 fxf_x 禁止,拒絕請求;否則將 fyf_y 併入 fxf_x,並以位元 OR 同步合併兩個集合:

comp[fx]←comp[fx]∣comp[fy],ban[fx]←ban[fx]∣ban[fy]\textit{comp}[f_x] \leftarrow \textit{comp}[f_x] \mathbin{|} \textit{comp}[f_y], \qquad \textit{ban}[f_x] \leftarrow \textit{ban}[f_x] \mathbin{|} \textit{ban}[f_y]

複雜度分析

  • 時間複雜度:O(n+m+q⋅(α(n)+n/w))\mathcal{O}(n + m + q \cdot (\alpha(n) + n / w)),其中 ww 為機器字長(大整數按字長進行位元運算)。
  • 空間複雜度:O(n2/w)\mathcal{O}(n^2 / w)(不計回傳陣列),兩個長度為 nn、寬度至多 nn 位元的位元遮罩陣列共占 O(n2)\mathcal{O}(n^2) 位元。

Code

方法一:併查集 + 枚舉限制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution:
def friendRequests(self, n: int, restrictions: List[List[int]], requests: List[List[int]]) -> List[bool]:
q = len(requests)

fa = list(range(n))
sz = [1] * n

def find(x: int) -> int:
if fa[x] != x:
fa[x] = find(fa[x])
return fa[x]

ans = [True] * q
for i, (x, y) in enumerate(requests):
fx, fy = find(x), find(y)
if fx == fy: # 已經在同一個連通分量中
continue

if sz[fx] < sz[fy]:
fx, fy = fy, fx

for u, v in restrictions:
u, v = find(u), find(v)
if u == fx and v == fy or u == fy and v == fx:
ans[i] = False
break
else:
fa[fy] = fx
sz[fx] += sz[fy]
return ans

方法二:併查集 + 位元遮罩(Bitmask)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution:
def friendRequests(self, n: int, restrictions: List[List[int]], requests: List[List[int]]) -> List[bool]:
q = len(requests)

fa = list(range(n))
sz = [1] * n

def find(x: int) -> int:
if fa[x] != x:
fa[x] = find(fa[x])
return fa[x]

# 用 bitmask 來維護連通分量中所有的點、以及禁止合併的點集合
comp = [1 << i for i in range(n)]
ban = [0 for _ in range(n)]
for x, y in restrictions:
ban[x] |= 1 << y
ban[y] |= 1 << x

ans = [True] * q
for i, (x, y) in enumerate(requests):
fx, fy = find(x), find(y)
if fx == fy: # 已經在同一個連通分量中
continue

if ban[fx] & comp[fy]: # 無法合併
ans[i] = False
continue

if sz[fx] < sz[fy]:
fx, fy = fy, fx

comp[fx] |= comp[fy]
ban[fx] |= ban[fy]
fa[fy] = fx
sz[fx] += sz[fy]
return ans