[Python] [272 ms,36 MB] Maintain connected components of the graph
Maintain two lists of sets connected_components and banned_by_comps to store the connected components the restrictions of nodes in each connected component. Mai
Maintain two lists of sets ```connected_components``` and ```banned_by_comps``` to store the connected components the restrictions of nodes in each connected component. Maintain a dictionary ```connected_comp_dict``` to map each node to its connected compoent. Update them when a new edge is added.\n```\nclass Solution:\n    def friendRequests(self, n: int, restrictions: List[List[int]], requests: List[List[int]]) -> List[bool]:        \n        result = [False for _ in requests]\n        \n        connected_components = [{i} for i in range(n)]\n        \n        connected_comp_dict = {}\n        for i in range(n):\n            connected_comp_dict[i] = i\n        \n        banned_by_comps = [set() for i in range(n)]\n        for res in restrictions:\n            banned_by_comps[res[0]].add(res[1])\n            banned_by_comps[res[1]].add(res[0])\n        for i,r in enumerate(requests):\n            n1, n2 = r[0], r[1]\n            c1, c2 = connected_comp_dict[n1], connected_comp_dict[n2]\n            if c1 == c2:\n                result[i] = True\n            else:\n                if not (connected_components[c1].intersection(banned_by_comps[c2]) or connected_components[c2].intersection(banned_by_comps[c1])):\n                    connected_components[c1].update(connected_components[c2])\n                    banned_by_comps[c1].update(banned_by_comps[c2])\n                    for node in connected_components[c2]:\n                        connected_comp_dict[node] = c1\n                    result[i] = True\n                \n        return result\n\n```
['Graph', 'Ordered Set', 'Python', 'Python3']