Problem p02762 (python)

Fixed submission:
n, m, k = map(int, input().split())

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 size(self, x):
        return -self.parents[self.find(x)]

    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 roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    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 ''.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

uf = UnionFind(n)

from collections import defaultdict
direct_f = defaultdict(int)
for i in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    direct_f[a] += 1
    direct_f[b] += 1
    uf.union(a, b)
ans = [uf.size(i) - direct_f[i] - 1 for i in range(n)]
for i in range(k):
    c, d = map(int, input().split())
    c -= 1
    d -= 1
    if uf.same(c,d):
        ans[c] -= 1
        ans[d] -= 1
print(*ans)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

n, m, k = map(int, input().split())
def connection(n):
    connect = defaultdict(set)
    for i in range(n):
        a, b = map(int, input().split())
        connect[a].add(b)
        connect[b].add(a)
    return connect
    
friends = connection(m)
blocks = connection(k)
groups = []
def dfs(start, check_set):
    seen = [False for i in range(n+1)]
    todo = []
    seen[start] = True
    todo.append(start)
    while todo:
        v = todo.pop()
        for w in check_set[v]:
            if not seen[w]:
                seen[w] = True
                todo.append(w)
    return seen

checked_friends = set()
groups = {}
group_num = 0
belong_group = {}
for person in range(n):
    if person in checked_friends:
        continue
    group_num += 1
    seen_friends = dfs(person, friends)
    seen_set = set(i for i, flag in enumerate(seen_friends) if flag)
    seen_len = len(seen_set)
    for i in seen_set:
        belong_group[i] = group_num
    checked_friends |= seen_set
    groups[group_num] = seen_set

print(' '.join(str(len(groups[belong_group[i]])-len(groups[belong_group[i]]&(friends[i]|blocks[i]))-1) for i in range(1, n+1)))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

n, m, k = map(int, input().split())
def connection(n):
    connect = defaultdict(set)
    for i in range(n):
        a, b = map(int, input().split())
        connect[a].add(b)
        connect[b].add(a)
    return connect
    
friends = connection(m)
blocks = connection(k)
groups = []
def dfs(start, check_set):
    seen = [False for i in range(n+1)]
    todo = []
    seen[start] = True
    todo.append(start)
    while todo:
        v = todo.pop()
        for w in check_set[v]:
            if not seen[w]:
                seen[w] = True
                todo.append(w)
    return seen

checked_friends = set()
groups = {}
group_num = 0
belong_group = {}
for person in range(n):
    if person in checked_friends:
        continue
    group_num += 1
    seen_friends = dfs(person, friends)
    seen_set = set(i for i, flag in enumerate(seen_friends) if flag)
    seen_len = len(seen_set)
    for i in seen_set:
        belong_group[i] = group_num
    not_checked_friends |= seen_set
    groups[group_num] = seen_set

print(' '.join(str(len(groups[belong_group[i]])-len(groups[belong_group[i]]&(friends[i]|blocks[i]))-1) for i in range(1, n+1)))