Problem: An SNS has N users - User 1, User 2, \cdots, User N.  Between these N users, there are some relationships - M _friendships_ and K _blockships_.  For each i = 1, 2, \cdots, M, there is a bidirectional friendship between User A_i and User B_i.  For each i = 1, 2, \cdots, K, there is a bidirectional blockship between User C_i and User D_i.  We define User a to be a _friend candidate_ for User b when all of the following four conditions are satisfied:    * a \neq b.   * There is not a friendship between User a and User b.   * There is not a blockship between User a and User b.   * There exists a sequence c_0, c_1, c_2, \cdots, c_L consisting of integers between 1 and N (inclusive) such that c_0 = a, c_L = b, and there is a friendship between User c_i and c_{i+1} for each i = 0, 1, \cdots, L - 1.  For each user i = 1, 2, ... N, how many friend candidates does it have?

Submission attempts (in order):
#!/usr/bin python3
# -*- coding: utf-8 -*-

class UnionFind():
    def __init__(self, n):      
        self.n = n
        
        self.parents = [i for i in range(n)]
        
        self.ranks = [0] * n
        
        self.sizes = [1] * n

    def find(self, x):          
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.ranks[x] < self.ranks[y]:
            self.parents[x] = y
            self.sizes[y] += self.sizes[x]
        else:
            self.parents[y] = x
            self.sizes[x] += self.sizes[y]
            if self.ranks[x]==self.ranks[y]:
                self.ranks[x] += 1

    def same(self, x, y):       
        return self.find(x) == self.find(y)

    def members(self, x):       
        root = self.find(x)
        return {i for i in range(self.n) if self.find(i) == root}

    def size(self, x):          
        root = self.find(x)
        return self.sizes[root]

    def roots(self):            
        return {i for i, x in enumerate(self.parents) if i == x}

    def group_count(self):      
        return len(self.roots())

    def all_group_members(self):      
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

################

def main():
    N, M, K = map(int, input().split())
    uf = UnionFind(N)
    dic = {i:set([i]) for i in range(N)}

    for i in range(M):
        a,b = map(int,input().split())
        uf.unite(a-1, b-1)
        dic[a-1].add(b-1)
        dic[b-1].add(a-1)

    for j in range(K):
        a,b = map(int,input().split())
        dic[a-1].add(b-1)
        dic[b-1].add(a-1)

    ret = [None]*N
    for i in range(N):
        cnt = 0
        print(uf.members(i))
        for x in uf.members(i):
            cnt += not x in dic[i]
        ret[i]=cnt
    print(" ".join(map(str,ret)))

if __name__ == '__main__':
    main()

#!/usr/bin python3
# -*- coding: utf-8 -*-

class UnionFind():
    def __init__(self, n):      
        self.n = n
        
        self.parents = [i for i in range(n)]
        
        self.ranks = [0] * n
        
        self.sizes = [1] * n

    def find(self, x):          
        if self.parents[x] == x:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.ranks[x] < self.ranks[y]:
            self.parents[x] = y
            self.sizes[y] += self.sizes[x]
        else:
            self.parents[y] = x
            self.sizes[x] += self.sizes[y]
            if self.ranks[x]==self.ranks[y]:
                self.ranks[x] += 1

    def same(self, x, y):       
        return self.find(x) == self.find(y)

    def members(self, x):       
        root = self.find(x)
        return {i for i in range(self.n) if self.find(i) == root}

    def size(self, x):          
        root = self.find(x)
        return self.sizes[root]

    def roots(self):            
        return {i for i, x in enumerate(self.parents) if i == x}

    def group_count(self):      
        return len(self.roots())

    def all_group_members(self):      
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

################

def main():
    N, M, K = map(int, input().split())
    uf = UnionFind(N)
    dic = {i:set([i]) for i in range(N)}
    fcnt = {i:0 for i in range(N)}

    for i in range(M):
        a,b = map(int,input().split())
        uf.unite(a-1, b-1)
        fcnt[a-1] += 1
        fcnt[b-1] += 1

    for j in range(K):
        a,b = map(int,input().split())
        dic[a-1].add(b-1)
        dic[b-1].add(a-1)

    ret = [None]*N
    for i in range(N):
        ret[i] = uf.size(i)-fcnt[i]
        for x in dic[i]:
            if uf.same(x,i):
                ret[i] -= 1

    print(" ".join(map(str,ret)))

if __name__ == '__main__':
    main()

Verdicts: Wrong Answer

Accepted