Problem p02762 (python)

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


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

# 友達関係をUFで用意
uf = UnionFind(N)

# 直接の友達のリスト
friend = [[] for i in range(N)]
# 直接のブロックのリスト
block = [[] for i in range(N)]

for i in range(M):
    A, B = map(int, input().split())
    uf.union(A-1, B-1)
    friend[A-1].append(B-1)
    friend[B-1].append(A-1)

for i in range(K):
    C, D = map(int, input().split())
    block[C-1].append(D-1)
    block[D-1].append(C-1)

ans = [0 for i in range(N)]

# 友達関係のグループの要素数ーグループ内の直接の友達の数ーグループ内の直接のブロックの数ー自分自身
for i in range(N):
    ans[i] += uf.size(i) -1
    for j in friend[i]:
        if uf.same(i,j):
            ans[i] -= 1
    for j in block[i]:
        if uf.same(i,j):
            ans[j] -= 1

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

Buggy submission (verdict: Runtime Error):
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 '\n'.join(
            '{}: {}'.format(r, self.members(r)) for r in self.roots())

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

# 友達関係をUFで用意
uf = UnionFind(N)

# 直接の友達のリスト
friend = [[] for i in range(N)]
# 直接のブロックのリスト
block = [[] for i in range(N)]

for i in range(M):
    A, B = map(int, input().split())
    uf.union(A-1, B-1)
    friend[A-1].append(B-1)
    friend[B-1].append(A-1)

for i in range(K):
    C, D = map(int, input().split())
    block[C-1].append(D-1)
    block[D-1].append(C-1)

ans = [0 for i in range(N)]

# 友達関係のグループの要素数ーグループ内の直接の友達の数ーグループ内の直接のブロックの数ー自分自身
for i in range(N):
    ans[i] += uf.size(i) -1
    for j in friend[i]:
        if uf.same(i,j):
            ans[i] -= 1
    for j in block[i]:
        if uf.same(i,j):
            ans[j] -= 1

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

Buggy submission (verdict: Runtime Error):
if __name__ == "__main__":
    resolve()

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



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

    # 友達関係をUFで用意
    uf = UnionFind(N)

    # 直接の友達のリスト
    friend = [[]for i in range(N)]
    # 直接のブロックのリスト
    block = [[] for i in range(N)]

    for i in range(M):
        A, B = map(int, input().split())
        uf.union(A-1,B-1)
        friend[A-1].append(B-1)
        friend[B-1].append(A-1)

    for i in range(K):
        C, D = map(int, input().split())
        block[C-1].append(D-1)
        block[D-1].append(C-1)

    ans = [0 for i in range(N)]

    # 友達関係のグループの要素数ーグループ内の直接の友達の数ーグループ内の直接のブロックの数ー自分自身
    for i in range(N):
        X = set(uf.members(i))
        Y = set(friend[i])
        Z = set(block[i])
        ans[i] = len(X) - len(X & Y) - len(X & Z) - 1

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