Problem p02762 (python)

Fixed submission:
class UnionFind:
    def __init__(self, N):
        self.N = N
        self.parents = [i for i in range(N)]
        self.sizes = [1] * N

    def root(self, x):
        if self.parents[x] == x:
            return x
        self.parents[x] = self.root(self.parents[x])
        return self.parents[x]

    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        if rx < ry:
            self.parents[ry] = rx
            self.sizes[rx] += self.sizes[ry]
            self.sizes[ry] = 0
        elif rx > ry:
            self.parents[rx] = ry
            self.sizes[ry] += self.sizes[rx]
            self.sizes[rx] = 0

    def same(self, x, y):
        return self.root(x) == self.root(y)
    
    def size(self, x):
        return self.sizes[self.root(x)]

N, M, K = list(map(int, input().split()))
AB_list = []
for _ in range(M):
    A, B = list(map(int, input().split()))
    AB_list.append([A - 1, B - 1])
CD_list = []
for _ in range(K):
    C, D = list(map(int, input().split()))
    CD_list.append([C - 1, D - 1])

uf = UnionFind(N)
for A, B in AB_list:
    uf.unite(A, B)

friends = [0] * N
for A, B in AB_list:
    friends[A] += 1
    friends[B] += 1
blocks = [0] * N
for C, D in CD_list:
    if uf.root(C) != uf.root(D):
        continue
    blocks[C] += 1
    blocks[D] += 1
answers = [0] * N
for i in range(N):
    answers[i] = uf.size(i) - 1 - friends[i] - blocks[i]

print(" ".join(map(str, answers)))

Buggy submission (verdict: Runtime Error):
N, M, K = list(map(int, input().split()))
AB_list = []
for _ in range(M):
    A, B = sorted(list(map(int, input().split())))
    AB_list.append([A - 1, B - 1])
CD_list = []
for _ in range(K):
    C, D = sorted(list(map(int, input().split())))
    CD_list.append([C - 1, D - 1])

parents = [i for i in range(N)]

def root(x):
    if parents[x] == x:
        return x
    parents[x] = root(parents[x])
    return parents[x]

def unite(x, y):
    rx = root(x)
    ry = root(y)
    if rx != ry:
        parents[rx] = ry

def same(x, y):
    return root(x) == root(y)

for A, B in AB_list:
    unite(A, B)
for i in range(N):
    root(i)

answers = [0] * N
for i in range(N):
    may_friends = parents.count(root(i)) - 1
    friends = sum(1 for x, y in AB_list if x == i or y == i)
    blocks = sum(1 for x, y in CD_list if (x == i or y == i) and root(x) == root(y))
    answers[i] = may_friends - friends - blocks

print(" ".join(map(str, answers)))

Buggy submission (verdict: Runtime Error):
N, M, K = list(map(int, input().split()))
AB_list = []
for _ in range(M):
    A, B = sorted(list(map(int, input().split())))
    AB_list.append([A - 1, B - 1])
CD_list = []
for _ in range(K):
    C, D = sorted(list(map(int, input().split())))
    CD_list.append([C - 1, D - 1])

parents = [i for i in range(N)]

def root(x):
    if parents[x] == x:
        return x
    parents[x] = root(parents[x])
    return parents[x]

def unite(x, y):
    rx = root(x)
    ry = root(y)
    if rx != ry:
        parents[rx] = ry

def same(x, y):
    return root(x) == root(y)

for A, B in AB_list:
    unite(A, B)

answers = [0] * N
for i in range(N):
    for j in range(N):
        if i == j:
            continue
        pair = sorted([i, j])
        if pair in AB_list:
            continue
        if pair in CD_list:
            continue
        if root(i) != root(j):
            continue
        answers[i] += 1

print(" ".join(map(str, answers)))