1
class UnionFindSet(object):6
while x <> self.data[x]:10
def union(self, x, y):11
self.data[self.find(x)] = self.find(y)14
for i in range(len(self.data)):15
self.data[i] = self.find(i)18
class Solution(object):19
def friendRequests(self, n, restrictions, requests):21
ret = [True] * len(requests)22
for k, [x, y] in enumerate(requests): # Process Requests Sequentially23
xh = uf.find(x) # backup the head of x for undo24
uf.union(x, y) # link [x, y] and verify if any restriction triggers25
for [i, j] in restrictions:26
if uf.find(i) == uf.find(j):29
if not ret[k]: # if any restriction triggers, undo