Problem p02762 (python)

Fixed submission:
import sys
def input(): return sys.stdin.readline().strip()
N,M,K = map(int,input().split())
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 l_in():
    return list(map(int, input().split()))

def print_l(l):
    print(' '.join(map(str, l)))

uni = UnionFind(N)
friends = []

for _ in range(M):
    a,b = l_in()
    uni.union(a-1, b-1)
    friends.append((a-1, b-1))

blocks = []

for _ in range(K):
    c,d = l_in()
    blocks.append((c-1, d-1))

res = [uni.size(i)-1 for i in range(N)]

for a,b in friends:
    res[a] -= 1
    res[b] -= 1

for c,d in blocks:
    if uni.same(c, d):
        res[c] -= 1
        res[d] -= 1

print_l(res)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
def input(): return sys.stdin.readline().strip()

class UnionFind:
    def __init__(self, n):
        self.par = list(range(n))
        self.rank = [0]*n
        self._size = [1]*n

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        self._size[x] += self._size[y]
        self._size[y] += self._size[x]

        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)

    def size(self, x):
        return self._size[self.find(x)]


def s_in():
    return input()

def n_in():
    return int(input())

def l_in():
    return list(map(int, input().split()))

def print_l(l):
    print(' '.join(map(str, l)))

N, M, K = l_in()

uni = UnionFind(N)

friends = []

for _ in range(M):
    a,b = l_in()
    uni.union(a-1, b-1)
    friends.append((a-1, b-1))

blocks = []

for _ in range(K):
    c,d = l_in()
    blocks.append((c-1, d-1))

res = [uni.size(i)-1 for i in range(N)]

for a,b in friends:
    res[a] -= 1
    res[b] -= 1

for c,d in blocks:
    if uni.same_check(c, d):
        res[c] -= 1
        res[d] -= 1

print_l(res)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size = [1]*(n+1)

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

    def union(self, x, y):
        if not self.same_check(x, y):
            x = self.find(x)
            y = self.find(y)
            self.size[x] += self.size[y]
            self.size[y] += self.size[x]

        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)


def s_in():
    return input()

def n_in():
    return int(input())

def l_in():
    return list(map(int, input().split()))


N, M, K = l_in()

uni = UnionFind(N)

friends = defaultdict(set)

for _ in range(N):
    a,b = l_in()
    uni.union(a-1, b-1)
    friends[a-1].add(b-1)
    friends[b-1].add(a-1)

blocks = defaultdict(set)

for _ in range(K):
    c,d = l_in()
    blocks[c-1].add(d-1)
    blocks[d-1].add(c-1)



res = []

for i in range(N):
    uni.find(i)
    n = uni.size[i]-1
    for f in friends[i]:
        if uni.same_check(i, f):
            n-=1

    for f in blocks[i]:
        if uni.same_check(i, f):
            n-=1


    res.append(n)

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