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):
import sys
from collections import defaultdict

sys.setrecursionlimit(2147483647)


class Graph:
    def __init__(self):
        self.graph = defaultdict(set)

    def interconnect(self, u, v):
        self.graph[u].add(v)
        self.graph[v].add(u)

    def dfs(self, start):
        seen = set()
        return self.dfs_recurse(start, seen)

    def dfs_recurse(self, v, seen):
        seen.add(v)
        for u in self.graph[v] - seen:
            self.dfs_recurse(u, seen)
        return seen


if __name__ == "__main__":
    n, m, k = map(int, input().split())

    friends = Graph()
    blocked = defaultdict(set)

    [friends.interconnect(*map(lambda x: int(x) - 1, input().split())) for _ in range(m)]
    for _ in range(k):
        c, d = map(lambda x: int(x) - 1, input().split())
        blocked[c - 1].add(d)
        blocked[d - 1].add(c)

    visited = [False] * n
    counts = [0] * n

    for v in range(n):
        if visited[v]:
            continue
        elems = friends.dfs(v)
        elems_cnt = len(elems)
        for i in elems:
            counts[i] = elems_cnt - len(elems & friends.graph[i]) - len(elems & blocked[i]) - 1
            visited[i] = True

    print(*counts)


from collections import defaultdict


def dfs(graph, start):
    visited, stack = set(), [start]
    while stack:
        vertex = stack.pop()
        if vertex not in visited:
            visited.add(vertex)
            stack.extend(graph[vertex] - visited)
    return visited

n, m, k = map(int, input().split())
AB = [map(int, input().split()) for _ in range(m)]
CD = [map(int, input().split()) for _ in range(k)]

graph_f = defaultdict(set)
graph_b = defaultdict(set)

for a, b in AB:
    graph_f[a].add(b)
    graph_f[b].add(a)

for c, d in CD:
    graph_b[c].add(d)
    graph_b[d].add(c)

res = [0] * n

for i in range(n):
    reachables = dfs(graph_f, i)
    for j in range(n):
        if i == j:
            continue
        else:
            if j in reachables and j not in graph_f[i] and j not in graph_b[i]:
                res[i] += 1

print(' '.join(map(str, res)))

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

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

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

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

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

    def size(self, x):
        return -self.parents[self.find(x)]


if __name__ == "__main__":
    n, m, k = map(int, input().split())

    u = UnionFind(n)
    f_cnts = [0] * n
    b_cnts = [0] * n

    for _ in range(m):
        x, y = map(lambda x: int(x) - 1, input().split())
        u.union(x, y)
        f_cnts[x] += 1
        f_cnts[y] += 1

    for _ in range(k):
        x, y = map(lambda x: int(x) - 1, input().split())
        if u.same(x, y):
            b_cnts[x] += 1
            b_cnts[y] += 1

    cnts = ((u.size(i) - f_cnts[i] - b_cnts[i] - 1) for i in range(n))
    print(' '.join(map(str, cnts)))


Verdicts: Wrong Answer

Wrong Answer

Accepted