Problem p02762 (python)

Fixed submission:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1] * (n + 1)
        self.rnk = [0] * (n + 1)

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

    def is_same_group(self, x, y):
        return self.get_root(x) == self.get_root(y)

    def count(self, x):
        return -self.root[self.get_root(x)]

    def unite(self, x, y):
        x = self.get_root(x)
        y = self.get_root(y)

        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1


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

f = UnionFind(N + 1)

friends = {}
for _ in range(M):
    a, b = map(int, input().split())
    if friends.get(a) is None:
        friends[a] = set([b])
    else:
        friends[a].add(b)
    if friends.get(b) is None:
        friends[b] = set([a])
    else:
        friends[b].add(a)
    f.unite(a, b)

blocks = {}
for _ in range(K):
    c, d = map(int, input().split())
    if blocks.get(c) is None:
        blocks[c] = set([d])
    else:
        blocks[c].add(d)
    if blocks.get(d) is None:
        blocks[d] = set([c])
    else:
        blocks[d].add(c)


def get_number_of_blocks_in_same_group(i):
    block = blocks.get(i, [])
    return len([j for j in block if f.is_same_group(i, j)])


for i in range(1, N + 1):
    count = f.count(i)
    friend = friends.get(i, [])
    num_friends = len(friend)
    num_blocks = get_number_of_blocks_in_same_group(i)
    ans = count - num_friends - num_blocks - 1
    # print('count', count, friend, num_friends, num_blocks)
    print(ans)

Buggy submission (verdict: Runtime Error):
import sys
import numpy as np


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1] * (n + 1)
        self.rnk = [0] * (n + 1)

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

    def is_same_group(self, x, y):
        return self.get_root(x) == self.get_root(y)

    def count(self, x):
        return -self.root[self.get_root(x)]

    def unite(self, x, y):
        x = self.get_root(x)
        y = self.get_root(y)

        if x == y:
            return
        elif self.rnk[x] > self.rnk[y]:
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if self.rnk[x] == self.rnk[y]:
                self.rnk[y] += 1


input = sys.stdin.buffer.readline
N, M, K = map(int, input().split())

F = np.zeros((N, N), dtype='bool')
B = np.zeros((N, N), dtype='bool')

uf = UnionFind(N)
for _ in range(M):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1  # 0-indexed
    F[a, b] = 1
    F[b, a] = 1
    uf.unite(a, b)

for _ in range(K):
    c, d = map(int, input().split())
    c, d = c - 1, d - 1  # 0-indexed
    B[c, d] = 1
    B[d, c] = 1

for i in range(N):
    cnt = 0
    for j in range(N):
        if i == j:
            continue
        if uf.is_same_group(i, j) and not F[i, j] and not B[i, j]:
            # print('i, j', i, j)
            cnt += 1
    print(cnt)

Buggy submission (verdict: Runtime Error):
import numpy as np
N, M, K = map(int, input().split())

friends = {}
for _ in range(M):
    a, b = map(int, input().split())
    # a, b = a - 1, b - 1
    # a, b = min(a, b), max(a, b)
    if friends.get(a) is None:
        friends[a] = set([b])
    else:
        friends[a].add(b)
    if friends.get(b) is None:
        friends[b] = set([a])
    else:
        friends[b].add(a)
blocks = {}
for _ in range(K):
    a, b = map(int, input().split())
    # a, b = a - 1, b - 1
    # a, b = min(a, b), max(a, b)
    # blocks.append((a, b))
    if blocks.get(a) is None:
        blocks[a] = set([b])
    else:
        blocks[a].add(b)
    if blocks.get(b) is None:
        blocks[b] = set([a])
    else:
        blocks[b].add(a)
# print('friends', friends)
# print('blocks', blocks)


def f(c, d):
    l = friends[c]
    # print('c, d, l', c, d, l)
    if len(l) == 0:
        return 0
    while len(l) > 0:
        ll = set()
        for i in l:
            if d in friends[i]:
                return 1
            # print('friends[i]', friends[i])
            ll = ll | friends[i]
        if len(ll) == 0:
            return 0
        print('ll', ll)
        l = ll
    return 0


ans = [0] * (N + 1)
for i in range(1, N + 1):
    if friends.get(i) is None:
        continue
    for j in range(i + 1, N + 1):
        if friends.get(j) is None:
            continue
        if i in friends[j]:
            continue
        if i in blocks[j]:
            continue
        # print('i j', i, j)
        v = f(i, j)
        ans[i] += v
        ans[j] += v
for i in ans[1:]:
    print(i)