Problem p02762 (python)

Fixed submission:
def get_root(s):
    if s != root[s]:
        root[s] = get_root(root[s])
        return root[s]
    return s

def unite(s, t):
    root_s = get_root(s)
    root_t = get_root(t)
    if not root_s == root_t:
        if rank[s] == rank[t]:
            root[root_t] = root_s
            rank[root_s] += 1
            union_count[root_s] += union_count[root_t]
        elif rank[s] > rank[t]:
            root[root_t] = root_s
            union_count[root_s] += union_count[root_t]
        else:
            root[root_s] = root_t
            union_count[root_t] += union_count[root_s]

def same(s, t):
    if get_root(s) == get_root(t):
        return True
    else:
        return False

n, m, k = map(int, input().split())
root = [i for i in range(n)]
rank = [1 for _ in range(n)]
union_count = [1 for _ in range(n)]
friend_count = [0] * n
for _ in range(m):
    a, b = map(int, input().split())
    unite(a - 1, b - 1)
    friend_count[a - 1] += 1
    friend_count[b - 1] += 1
ans = [-1] * n
for _ in range(k):
    c, d = map(int, input().split())
    if same(c - 1, d - 1):
        ans[c - 1] -= 1
        ans[d - 1] -= 1
for i in range(n):
    r = get_root(i)
    ans[i] += union_count[r] - friend_count[i]
print(" ".join(map(str, ans)))

Buggy submission (verdict: Runtime Error):
from collections import deque
q = deque([])

def get_root(i):
    while True:
        if parent[i] == -1:
            return i
        i = parent[i]

def bfs(i):
    global l
    c = 0
    q.append(i)
    while True:
        if len(q) == 0:
            break
        p = q.popleft()
        l.append(p)
        c += 1
        if not children[p] == -1:
            for j in children[p]:
                q.append(j)
    return c

n, m, k = map(int, input().split())
parent = [-1] * n
children = [-1] * n
d = [0] * n
root = []
rank = []
friend = ["a"] * n
block = ["a"] * n
for _ in range(m):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    if friend[x] == "a":
        friend[x] = [y]
    else:
        friend[x].append(y)
    if friend[y] == "a":
        friend[y] = [x]
    else:
        friend[y].append(x)
    if d[x] == d[y] == 0:
        root.append(x)
        rank.append(1)
        children[x] = [y]
        parent[y] = x
        d[x] = 1
        d[y] = 1
    elif d[x] == 1 and d[y] == 0:
        r = get_root(x)
        children[r].append(y)
        parent[y] = r
        d[y] = 1
    elif d[x] == 0 and d[y] == 1:
        r = get_root(y)
        children[r].append(x)
        parent[x] = r
        d[x] = 1
    else:
        s = get_root(x)
        t = get_root(y)
        if not s == t:
            i = rank[root.index(s)]
            j = rank[root.index(t)]
            if i >= j:
                parent[t] = s
                children[s].append(t)
                if i == j:
                    rank[root.index(s)] += 1
            else:
                parent[s] = t
                children[t].append(s)
for _ in range(k):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    if block[x] == "a":
        block[x] = [y]
    else:
        block[x].append(y)
    if block[y] == "a":
        block[y] = [x]
    else:
        block[y].append(x)

c = [0] * len(root)
ans = []
for i in range(n):
    r = get_root(i)
    if c[root.index(r)] == 0:
        l = []
        bfs(r)
        c[root.index(r)] = l
    a = len(c[root.index(r)])
    if friend[i] != "a":
        for j in friend[i]:
            if j in c[root.index(r)]:
                a -= 1
    if block[i] != "a":
        for j in block[i]:
            if j in c[root.index(r)]:
                a -= 1
    ans.append(a - 1)
print(" ".join(map(str, ans)))