72
72
73
73
## 解法
74
74
75
- ### 方法一
75
+ ### 方法一:并查集
76
+
77
+ 我们可以用并查集来维护朋友关系,然后对于每个请求,判断是否满足限制条件。
78
+
79
+ 对于当前请求的两个人 $(u, v)$,如果他们已经是朋友,那么可以直接接受请求;否则,我们遍历限制条件,如果存在限制条件 $(x, y)$,使得 $u$ 和 $x$ 互为朋友并且 $v$ 和 $y$ 互为朋友,或者 $u$ 和 $y$ 互为朋友并且 $v$ 和 $x$ 互为朋友,那么就不能接受请求。
80
+
81
+ 时间复杂度 $O(q \times m \times \log(n))$,空间复杂度 $O(n)$。其中 $q$ 和 $m$ 分别是请求的数量和限制条件的数量。
76
82
77
83
<!-- tabs:start -->
78
84
@@ -81,29 +87,27 @@ class Solution:
81
87
def friendRequests (
82
88
self , n : int , restrictions : List[List[int ]], requests : List[List[int ]]
83
89
) -> List[bool ]:
84
- p = list (range (n))
85
-
86
- def find (x ):
90
+ def find (x : int ) -> int :
87
91
if p[x] != x:
88
92
p[x] = find(p[x])
89
93
return p[x]
90
94
95
+ p = list (range (n))
91
96
ans = []
92
- i = 0
93
97
for u, v in requests:
94
- if find(u) == find(v):
98
+ pu, pv = find(u), find(v)
99
+ if pu == pv:
95
100
ans.append(True )
96
101
else :
97
- valid = True
102
+ ok = True
98
103
for x, y in restrictions:
99
- if (find(u) == find(x) and find(v) == find(y)) or (
100
- find(u) == find(y) and find(v) == find(x)
101
- ):
102
- valid = False
104
+ px, py = find(x), find(y)
105
+ if (pu == px and pv == py) or (pu == py and pv == px):
106
+ ok = False
103
107
break
104
- ans.append(valid )
105
- if valid :
106
- p[find(u) ] = find(v)
108
+ ans.append(ok )
109
+ if ok :
110
+ p[pu ] = pv
107
111
return ans
108
112
```
109
113
@@ -116,27 +120,25 @@ class Solution {
116
120
for (int i = 0 ; i < n; ++ i) {
117
121
p[i] = i;
118
122
}
119
- boolean [] ans = new boolean [requests. length];
120
- int i = 0 ;
121
- for (int [] req : requests) {
122
- int u = req[0 ], v = req[1 ];
123
- if (find(u) == find(v)) {
124
- ans[i++ ] = true ;
123
+ int m = requests. length;
124
+ boolean [] ans = new boolean [m];
125
+ for (int i = 0 ; i < m; ++ i) {
126
+ int u = requests[i][0 ], v = requests[i][1 ];
127
+ int pu = find(u), pv = find(v);
128
+ if (pu == pv) {
129
+ ans[i] = true ;
125
130
} else {
126
- boolean valid = true ;
127
- for (int [] res : restrictions) {
128
- int x = res[0 ], y = res[1 ];
129
- if ((find(u) == find(x) && find(v) == find(y))
130
- || (find(u) == find(y) && find(v) == find(x))) {
131
- valid = false ;
131
+ boolean ok = true ;
132
+ for (var r : restrictions) {
133
+ int px = find(r[0 ]), py = find(r[1 ]);
134
+ if ((pu == px && pv == py) || (pu == py && pv == px)) {
135
+ ok = false ;
132
136
break ;
133
137
}
134
138
}
135
- if (valid) {
136
- p[find(u)] = find(v);
137
- ans[i++ ] = true ;
138
- } else {
139
- ans[i++ ] = false ;
139
+ if (ok) {
140
+ ans[i] = true ;
141
+ p[pu] = pv;
140
142
}
141
143
}
142
144
}
@@ -155,75 +157,109 @@ class Solution {
155
157
``` cpp
156
158
class Solution {
157
159
public:
158
- vector<int > p;
159
-
160
160
vector<bool > friendRequests(int n, vector<vector<int >>& restrictions, vector<vector<int >>& requests) {
161
- p.resize(n);
162
- for (int i = 0; i < n; ++i) p[i] = i;
161
+ vector<int > p(n);
162
+ iota(p.begin(), p.end(), 0);
163
+ function<int(int)> find = [ &] (int x) {
164
+ if (p[ x] != x) {
165
+ p[ x] = find(p[ x] );
166
+ }
167
+ return p[ x] ;
168
+ };
163
169
vector<bool > ans;
164
170
for (auto& req : requests) {
165
171
int u = req[ 0] , v = req[ 1] ;
166
- if (find(u) == find(v))
172
+ int pu = find(u), pv = find(v);
173
+ if (pu == pv) {
167
174
ans.push_back(true);
168
- else {
169
- bool valid = true;
170
- for (auto& res : restrictions) {
171
- int x = res [0], y = res [1];
172
- if ((find(u) == find(x) && find(v) == find(y)) || (find(u) == find(y) && find(v) == find(x) )) {
173
- valid = false;
175
+ } else {
176
+ bool ok = true;
177
+ for (auto& r : restrictions) {
178
+ int px = find(r [ 0] ), py = find(r [ 1] ) ;
179
+ if ((pu == px && pv == py) || (pu == py && pv == px )) {
180
+ ok = false;
174
181
break;
175
182
}
176
183
}
177
- ans.push_back(valid);
178
- if (valid) p[find(u)] = find(v);
184
+ ans.push_back(ok);
185
+ if (ok) {
186
+ p[ pu] = pv;
187
+ }
179
188
}
180
189
}
181
190
return ans;
182
191
}
183
-
184
- int find (int x) {
185
- if (p[ x] != x) p[ x] = find(p[ x] );
186
- return p[ x] ;
187
- }
188
192
};
189
193
```
190
194
191
195
```go
192
- var p []int
193
-
194
- func friendRequests(n int, restrictions [][]int, requests [][]int) []bool {
195
- p = make([]int, n)
196
- for i := 0; i < n; i++ {
196
+ func friendRequests(n int, restrictions [][]int, requests [][]int) (ans []bool) {
197
+ p := make([]int, n)
198
+ for i := range p {
197
199
p[i] = i
198
200
}
199
- var ans []bool
201
+ var find func(int) int
202
+ find = func(x int) int {
203
+ if p[x] != x {
204
+ p[x] = find(p[x])
205
+ }
206
+ return p[x]
207
+ }
200
208
for _, req := range requests {
201
- u, v := req[0], req[1]
202
- if find(u) == find(v) {
209
+ pu, pv := find( req[0]), find( req[1])
210
+ if pu == pv {
203
211
ans = append(ans, true)
204
212
} else {
205
- valid := true
206
- for _, res := range restrictions {
207
- x, y := res [0], res [1]
208
- if (find(u) == find(x) && find(v) == find(y)) || (find(u) == find(y) && find(v) == find(x)) {
209
- valid = false
213
+ ok := true
214
+ for _, r := range restrictions {
215
+ px, py := find(r [0]), find(r [1])
216
+ if px == pu && py == pv || px == pv && py == pu {
217
+ ok = false
210
218
break
211
219
}
212
220
}
213
- ans = append(ans, valid )
214
- if valid {
215
- p[find(u) ] = find(v)
221
+ ans = append(ans, ok )
222
+ if ok {
223
+ p[pv ] = pu
216
224
}
217
225
}
218
226
}
219
- return ans
227
+ return
220
228
}
229
+ ```
221
230
222
- func find(x int) int {
223
- if p[x] != x {
224
- p[x] = find(p[x])
225
- }
226
- return p[x]
231
+ ``` ts
232
+ function friendRequests(n : number , restrictions : number [][], requests : number [][]): boolean [] {
233
+ const p: number [] = Array .from ({ length: n }, (_ , i ) => i );
234
+ const find = (x : number ): number => {
235
+ if (p [x ] !== x ) {
236
+ p [x ] = find (p [x ]);
237
+ }
238
+ return p [x ];
239
+ };
240
+ const ans: boolean [] = [];
241
+ for (const [u, v] of requests ) {
242
+ const pu = find (u );
243
+ const pv = find (v );
244
+ if (pu === pv ) {
245
+ ans .push (true );
246
+ } else {
247
+ let ok = true ;
248
+ for (const [x, y] of restrictions ) {
249
+ const px = find (x );
250
+ const py = find (y );
251
+ if ((px === pu && py === pv ) || (px === pv && py === pu )) {
252
+ ok = false ;
253
+ break ;
254
+ }
255
+ }
256
+ ans .push (ok );
257
+ if (ok ) {
258
+ p [pu ] = pv ;
259
+ }
260
+ }
261
+ }
262
+ return ans ;
227
263
}
228
264
```
229
265
0 commit comments