forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
28 lines (26 loc) · 841 Bytes
/
Solution.py
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
class Solution:
def friendRequests(
self, n: int, restrictions: List[List[int]], requests: List[List[int]]
) -> List[bool]:
p = list(range(n))
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
ans = []
i = 0
for u, v in requests:
if find(u) == find(v):
ans.append(True)
else:
valid = True
for x, y in restrictions:
if (find(u) == find(x) and find(v) == find(y)) or (
find(u) == find(y) and find(v) == find(x)
):
valid = False
break
ans.append(valid)
if valid:
p[find(u)] = find(v)
return ans