Problem statement

https://binarysearch.com/problems/No-New-Friends/

Solution

It is NOT about connected components. All we need to do is put all elements from pairs to set and then check that the length of this set is n.

Complexity

It is O(m) for time and O(n) for space.

Code

class Solution:
    def solve(self, n, friends):
        d = set()
        for x, y in friends:
            d |= set([x, y])

        return len(d) == n