Problem p02762 (python)

Fixed submission:
def dfs(graph, start):
    visited = set()
    stack = [start]

    while stack:
        v = stack.pop()
        if v not in visited:
            stack.extend(graph[v])
            visited.add(v)
            yield v


def read_graph(nodes, edges):
    graph = [[] for _ in range(nodes)]
    for _ in range(edges):
        a, b = (int(x) - 1 for x in input().split())
        graph[a].append(b)
        graph[b].append(a)

    return graph


def count_candidates(n, counts, groups, block, friend):
    for i in range(n):
        count = counts[groups[i]]
        for v in block[i]:
            if groups[i] == groups[v]:
                count -= 1

        count -= len(friend[i])
        count -= 1
        yield count


def main():
    n, m, k = map(int, input().split())
    friend = read_graph(n, m)
    block = read_graph(n, k)

    groups = [-1 for _ in range(n)]
    counts = []
    i = 0
    group_id = 0
    while i < n:
        if groups[i] != -1:
            i += 1
            continue

        for j, v in enumerate(dfs(friend, i)):
            groups[v] = group_id

        counts.append(j + 1)
        group_id += 1

    print(" ".join(str(i) for i in count_candidates(n, counts, groups, block, friend)))


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
def dfs(graph, start):
    visited = set([start])
    stack = [start]

    while stack:
        v = stack[-1]
        for w in graph[v]:
            if w not in visited:
                break

        if w in visited:
            stack.pop()
            yield v
        else:
            stack.append(w)
            visited.add(w)


def read_graph(nodes, edges):
    graph = [[] for _ in range(nodes)]
    for _ in range(edges):
        a, b = (int(x) - 1 for x in input().split())
        graph[a].append(b)
        graph[b].append(a)

    return graph


def count_candidates(n, counts, groups, block, friend):
    for i in range(n):
        count = counts[groups[i]]
        for v in block[i]:
            if groups[i] == groups[v]:
                count -= 1

        count -= len(friend[i])
        count -= 1
        yield count


def main():
    n, m, k = map(int, input().split())
    friend = read_graph(n, m)
    block = read_graph(n, k)

    groups = [-1 for _ in range(n)]
    counts = []
    i = 0
    group_id = 0
    while i < n:
        if groups[i] != -1:
            i += 1
            continue

        for j, v in enumerate(dfs(friend, i)):
            groups[v] = group_id

        counts.append(j + 1)
        group_id += 1

    print(" ".join(str(i) for i in count_candidates(n, counts, groups, block, friend)))


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import collections


def friend_candidates(i, friends, visited=None):
    if visited is None:
        visited = set()

    for j in friends[i]:
        if j not in visited:
            yield j
            visited.add(j)
            yield from friend_candidates(j, friends, visited=visited)


N, M, K = map(int, input().split())

friends = collections.defaultdict(list)
for _ in range(M):
    A, B = map(int, input().split())
    friends[A].append(B)
    friends[B].append(A)

blockings = collections.defaultdict(list)
for _ in range(K):
    C, D = map(int, input().split())
    blockings[C].append(D)
    blockings[D].append(C)


table = {}
for i in range(1, N + 1):
    if i in table:
        continue

    candidates = list(friend_candidates(i, friends))
    for j in candidates:
        table[j] = candidates


print(
    " ".join(
        str(
            len(table[i])
            - len(friends[i])
            - len(set(blockings[i]).intersection(table[i]))
            - 1
        )
        for i in range(1, N + 1)
    )
)