Problem p02762 (python)

Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n+1))
        self.rank = [0] * (n+1)
        self.size = [1]*(n+1)

    # 検索
    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        else:
            if self.size[x] < self.size[y]:
                self.parent[x] = y
                self.size[y] += self.size[x]
            else:
                self.parent[y] = x
                self.size[x] += self.size[y]

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

    def cnt(self, x):
        # サイズの取得
        return self.size[self.find(x)]


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

union_find = UnionFind(n)
num_friends = [0]*(n+1)
num_block = [0]*(n+1)
for i in range(m):
    a, b = map(int, input().split())
    num_friends[a] += 1
    num_friends[b] += 1
    union_find.union(a, b)
# cd = [[_c, _d] for (_c, _d) in map(int, input().split())]
for i in range(k):
    # c[i], d[i] = map(int, input().split())
    c, d = map(int, input().split())
    if union_find.same_check(c, d):
        num_block[c] += 1
        num_block[d] += 1


num_suggest_friend = [0]*(n+1)  # 自分自身を引いておくので-1で初期化

for i in range(1, n+1):
    num_suggest_friend[i] = union_find.cnt(i) - \
        num_friends[i] - num_block[i] - 1

print(*num_suggest_friend[1:], sep=' ')

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
import numpy as np


class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    # 検索
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    # 併合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

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


n, m, k = list(map(int, input().split()))
a = [0]*(n+1)
b = [0]*(n+1)
c = [0]*(k+1)
d = [0]*(k+1)
union_find = UnionFind(n)
num_friends = [0]*(n+1)
for i in range(m):
    a[i], b[i] = map(int, input().split())
    num_friends[a[i]] += 1
    num_friends[b[i]] += 1
    union_find.union(a[i], b[i])
for i in range(k):
    c[i], d[i] = map(int, input().split())

# frinds_mat = np.zeros([n, n])
# for i in range(m):
#     # a[i]とb[i]は友達
#     frinds_mat[a[i]-1, b[i]-1] = 1
#     frinds_mat[b[i]-1, a[i]-1] = 1
num_suggest_friend = [-1]*(n+1)  # 自分自身を引いておくので-1で初期化
for i in range(1, n+1):
    for j in range(1, n+1):
        if (union_find.same_check(i, j)):
            num_suggest_friend[i] += 1
# print(*num_suggest_friend[1:], sep=' ')


for i in range(1, n+1):
    num_suggest_friend[i] -= num_friends[i]
# print(*num_suggest_friend[1:], sep=' ')
for c_, d_ in zip(c, d):
    if union_find.same_check(c_, d_):
        num_suggest_friend[c_] -= 1
        num_suggest_friend[d_] -= 1

print(*num_suggest_friend[1:], sep=' ')