Problem p02762 (python)

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

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

    # xとyの属する集合の併合
    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        # sizeの大きいほうがx
        if self.parent[x] > self.parent[y]:
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.parent[y] = x

    # xとyが同じ集合に属するかの判定
    def same(self, x, y):
        return self.find(x) == self.find(y)

    # xが属する集合の個数
    def size(self, x):
        return -self.parent[self.find(x)]


n, m, k = map(int, input().split())
num_friends = [0 for _ in range(n + 1)]
blocks = [[] for _ in range(n + 1)]
uf = UnionFind(n + 1)

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

for i in range(k):
    c, d = map(int, input().split())
    blocks[c].append(d)
    blocks[d].append(c)

out = ''
for i in range(1, n + 1):
    ans = uf.size(i) - num_friends[i] - 1
    for block in blocks[i]:
        if uf.same(i, block):
            ans -= 1
    out += str(ans) + ' '
print(out.strip())

Buggy submission (verdict: Runtime Error):
def dfs(g_idx, idx):
    if gps[idx] != 'o':
        return
    gps[idx] = g_idx
    mbs[g_idx] += 1
    for f in fs[idx]:
        dfs(g_idx, f)


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

fs = {}
bs = {}
for _ in range(m):
    a, b = map(int, input().split())
    if a in fs:
        fs[a].append(b)
    else:
        fs[a] = [b]
    if b in fs:
        fs[b].append(a)
    else:
        fs[b] = [a]
for _ in range(k):
    c, d = map(int, input().split())
    if c in bs:
        bs[c].append(d)
    else:
        bs[c] = [d]
    if d in bs:
        bs[d].append(c)
    else:
        bs[d] = [c]

g_idx = 0
gps = ['o'] * (n + 1)
mbs = {}

for idx in range(1, n + 1):
    if gps[idx] != 'o':
        continue
    gps[idx] = g_idx
    mbs[g_idx] = 1
    for f in fs.get(idx, []):
        dfs(g_idx, f)
    g_idx += 1

out = ''
for idx in range(1, n + 1):
    ans = mbs[gps[idx]] - 1 - len(fs.get(idx, []))
    for b_idx in bs.get(idx, []):
        if gps[b_idx] == gps[idx]:
            ans -= 1
    out += str(ans) + ' '
print(out.rstrip())

Buggy submission (verdict: Runtime Error):
def dfs(g_idx, idx):
    if gps[idx] != 'o':
        return
    gps[idx] = g_idx
    mbs[g_idx] += 1
    for f in fs[idx]:
        dfs(g_idx, f)


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

fs = {}
bs = {}
for _ in range(m):
    a, b = map(int, input().split())
    if a in fs:
        fs[a].append(b)
    else:
        fs[a] = [b]
    if b in fs:
        fs[b].append(a)
    else:
        fs[b] = [a]
for _ in range(k):
    c, d = map(int, input().split())
    if c in bs:
        bs[c].append(d)
    else:
        bs[c] = [d]
    if d in bs:
        bs[d].append(c)
    else:
        bs[d] = [c]

g_idx = 0
gps = ['o'] * (n + 1)
mbs = {}

for idx in range(1, n + 1):
    if gps[idx] != 'o':
        continue
    gps[idx] = g_idx
    mbs[g_idx] = 1
    for f in fs[idx]:
        dfs(g_idx, f)
    g_idx += 1

out = ''
for idx in range(1, n + 1):
    ans = mbs[gps[idx]] - 1 - len(fs[idx])
    for b_idx in bs.get(idx, []):
        if gps[b_idx] == gps[idx]:
            ans -= 1
    out += str(ans) + ' '
print(out.rstrip())