Problem Statement
題目簡述
有 n n n 位編號為 0 0 0 到 n − 1 n - 1 n − 1 的使用者,初始彼此皆非好友。給定限制清單 restrictions \textit{restrictions} restrictions ,其中 [ x i , y i ] [x_i, y_i] [ x i , y i ] 表示 x i x_i x i 與 y i y_i y i 不能直接或間接成為好友。
依序處理好友請求清單 requests \textit{requests} requests ,其中 [ u j , v j ] [u_j, v_j] [ u j , v j ] 表示 u j u_j u j 與 v j v_j v j 請求成為好友:若同意後不會違反任何限制,則請求成功且兩人成為直接好友;否則請求失敗且關係不變。回傳布林陣列表示每個請求是否成功。
Constraints
約束條件
2 ≤ n ≤ 1000 2 \le n \le 1000 2 ≤ n ≤ 1 0 0 0
0 ≤ restrictions.length ≤ 1000 0 \le \textit{restrictions.length} \le 1000 0 ≤ restrictions.length ≤ 1 0 0 0 ,restrictions [ i ] . length = 2 \textit{restrictions}[i].\text{length} = 2 restrictions [ i ] . length = 2
0 ≤ x i , y i < n 0 \le x_i, y_i < n 0 ≤ x i , y i < n ,x i ≠ y i x_i \ne y_i x i = y i ,所有限制對互不相同
1 ≤ requests.length ≤ 1000 1 \le \textit{requests.length} \le 1000 1 ≤ requests.length ≤ 1 0 0 0 ,requests [ j ] . length = 2 \textit{requests}[j].\text{length} = 2 requests [ j ] . length = 2
0 ≤ u j , v j < n 0 \le u_j, v_j < n 0 ≤ u j , v j < n ,u j ≠ v j u_j \ne v_j u j = v j ,所有請求對互不相同
思路:併查集維護連通分量
好友關係具有傳遞性,「直接或間接成為好友」等價於兩位使用者位於同一個連通分量中。因此用併查集(Union-Find) 維護當前的連通分量(搭配路徑壓縮與按大小合併)。
處理請求 ( x , y ) (x, y) ( x , y ) 時,先找到兩者的代表元素 f x = find ( x ) f_x = \text{find}(x) f x = find ( x ) 、f y = find ( y ) f_y = \text{find}(y) f y = find ( y ) :
若 f x = f y f_x = f_y f x = f y :兩人原本就在同一連通分量,請求直接成功,不會引入新的連通關係。
若 f x ≠ f y f_x \ne f_y f x = f y :一旦合併,唯一新增的連通對就是「分量 f x f_x f x 內的人」與「分量 f y f_y f y 內的人」之間的配對。因此合併前只需判斷這兩個分量之間是否橫跨任何一條限制;若無衝突才合併。
以下兩種做法的差別,在於如何檢查兩個分量之間是否存在衝突。
方法一:併查集 + 枚舉限制
由於請求數與限制數皆不超過 1000 1000 1 0 0 0 ,可以直接枚舉 restrictions \textit{restrictions} restrictions 中的每一對 ( u , v ) (u, v) ( u , v ) :
查詢各自所屬的代表元素 find ( u ) \text{find}(u) find ( u ) 與 find ( v ) \text{find}(v) find ( v ) 。
若兩者恰好對應 { f x , f y } \{f_x, f_y\} { f x , f y } ,表示合併 f x f_x f x 與 f y f_y f y 會讓 u u u 與 v v v 變成間接好友,該請求必須拒絕。
若所有限制皆未觸發衝突,則將較小的分量併入較大的分量。
複雜度分析
時間複雜度:O ( n + q ⋅ m ⋅ α ( n ) ) \mathcal{O}(n + q \cdot m \cdot \alpha(n)) O ( n + q ⋅ m ⋅ α ( n ) ) ,其中 m = ∣ restrictions ∣ m = |\textit{restrictions}| m = ∣ restrictions ∣ 、q = ∣ requests ∣ q = |\textit{requests}| q = ∣ requests ∣ ,α \alpha α 為反阿克曼函數。
空間複雜度:O ( n ) \mathcal{O}(n) O ( n ) (不計回傳陣列)。
方法二:併查集 + 位元遮罩(Bitmask)
觀察 n ≤ 1000 n \le 1000 n ≤ 1 0 0 0 ,可利用 Python 任意精度整數將每個節點集合壓成 n n n 位元的位元遮罩,改以位元運算在 O ( n / w ) \mathcal{O}(n / w) O ( n / w ) 時間內完成衝突檢查與集合合併。
為每個連通分量的代表元素 r r r 維護兩個位元集合:
comp [ r ] \textit{comp}[r] comp [ r ] :分量 r r r 內所有人的集合,初始 comp [ i ] = 2 i \textit{comp}[i] = 2^i comp [ i ] = 2 i (僅包含自己)。
ban [ r ] \textit{ban}[r] ban [ r ] :與分量 r r r 內至少一人存在限制關係的所有人的集合,初始將每條限制 ( x , y ) (x, y) ( x , y ) 雙向加入 ban [ x ] \textit{ban}[x] ban [ x ] 與 ban [ y ] \textit{ban}[y] ban [ y ] 。
檢查 f x f_x f x 與 f y f_y f y 能否合併時,只需判斷 ban [ f x ] & comp [ f y ] \textit{ban}[f_x] \mathbin{\&} \textit{comp}[f_y] ban [ f x ] & comp [ f y ] 是否非零:若非零,表示分量 f y f_y f y 裡有人被分量 f x f_x f x 禁止,拒絕請求;否則將 f y f_y f y 併入 f x f_x f x ,並以位元 OR 同步合併兩個集合:
comp [ f x ] ← comp [ f x ] ∣ comp [ f y ] , ban [ f x ] ← ban [ f x ] ∣ ban [ f y ] \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]
comp [ f x ] ← comp [ f x ] ∣ comp [ f y ] , ban [ f x ] ← ban [ f x ] ∣ ban [ f y ]
複雜度分析
時間複雜度:O ( n + m + q ⋅ ( α ( n ) + n / w ) ) \mathcal{O}(n + m + q \cdot (\alpha(n) + n / w)) O ( n + m + q ⋅ ( α ( n ) + n / w ) ) ,其中 w w w 為機器字長(大整數按字長進行位元運算)。
空間複雜度:O ( n 2 / w ) \mathcal{O}(n^2 / w) O ( n 2 / w ) (不計回傳陣列),兩個長度為 n n n 、寬度至多 n n n 位元的位元遮罩陣列共占 O ( n 2 ) \mathcal{O}(n^2) 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] 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