Problem p02762 (python)

Fixed submission:
from collections import defaultdict
import sys
import time

sys.setrecursionlimit(2000)
N, M, K = [int(i) for i in input().split(" ")]

t = time.time()
class UnionNode():
    def __init__(self, number):
        self.number = number
        self.volume = 1
        self.master = None

    @property
    def root(self):
        if self.master:
            self.master = self.master.root
            return self.master
        else:
            return self

    def unite(self, target):
        seroot = self.root
        taroot = target.root
        if seroot.number == taroot.number:
            pass
        else:
            #print(f"unite {target.rootnum}({target.root.volume}), {self.rootnum}({self.root.volume})")
            if seroot.number < taroot.number:
                seroot.volume += taroot.volume
                taroot.master = seroot
            else:
                taroot.volume += seroot.volume
                seroot.master = taroot

nodes = [UnionNode(i) for i in range(N)]

outlist = defaultdict(lambda :[])

for i in range(M):
    s = input()
    a, b = s.split(" ")
    a = int(a) - 1
    b = int(b) - 1
    nodes[a].unite(nodes[b])
    outlist[a].append(b)
    outlist[b].append(a)

for i in range(K):
    s = input()
    a, b = s.split(" ")
    a = int(a) - 1
    b = int(b) - 1
    outlist[a].append(b)
    outlist[b].append(a)

result = []
for i in range(N):
    rootnode = nodes[i].root
    ret = rootnode.volume - 1
    for out in outlist[i]:
        if nodes[out].root.number == rootnode.number:
            ret -=1
    result.append(ret)

#for i in range(100):
#    print(result[i])

print(" ".join([str(i) for i in result]))

#print(time.time() - t)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
import sys
import time

sys.setrecursionlimit(2000)
N, M, K = [int(i) for i in input().split(" ")]

t = time.time()
class UnionNode():
    def __init__(self, number):
        self.number = number
        self.volume = 1
        self.master = None

    @property
    def root(self):
        if self.master:
            self.master = self.master.root
            return self.master
        else:
            return self

    def unite(self, target):
        seroot = self.root
        taroot = target.root
        if seroot.number == taroot.number:
            pass
        else:
            #print(f"unite {target.rootnum}({target.root.volume}), {self.rootnum}({self.root.volume})")
            if seroot.volume < taroot.volume:
                seroot.volume += taroot.volume
                taroot.master = seroot
            else:
                taroot.volume += seroot.volume
                seroot.master = taroot

nodes = [UnionNode(i) for i in range(N)]

outlist = defaultdict(lambda :[])

for i in range(M):
    s = input()
    a, b = s.split(" ")
    a = int(a) - 1
    b = int(b) - 1
    nodes[a].unite(nodes[b])
    outlist[a].append(b)
    outlist[b].append(a)

for i in range(K):
    s = input()
    a, b = s.split(" ")
    a = int(a) - 1
    b = int(b) - 1
    outlist[a].append(b)
    outlist[b].append(a)

result = []
for i in range(N):
    rootnode = nodes[i].root
    ret = rootnode.volume - 1
    for out in outlist[i]:
        if nodes[out].root.number == rootnode.number:
            ret -=1
    result.append(ret)

#for i in range(100):
#    print(result[i])

print(" ".join([str(i) for i in result]))

#print(time.time() - t)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
import time

N, M, K = [int(i) for i in input().split(" ")]

class UnionNode():
    def __init__(self, number):
        self.number = number
        self.volume = 1
        self.master = None

    @property
    def rootnum(self):
        return self.root.number

    @property
    def root(self):
        if self.master:
            return self.master.root
        else:
            return self

    def same(self, target):
        if self.root == target.root:
            return True
        else:
            return False

    def unite(self, target):
        if self.rootnum == target.rootnum:
            pass
        else:
            #print(f"unite {target.rootnum}({target.root.volume}), {self.rootnum}({self.root.volume})")
            self.root.volume += target.root.volume
            target.root.master = self.root

nodes = [UnionNode(i) for i in range(N)]

outlist = defaultdict(lambda :[])

for i in range(M):
    s = input()
    a, b = s.split(" ")
    a = int(a) - 1
    b = int(b) - 1
    nodes[a].unite(nodes[b])
    outlist[a].append(b)
    outlist[b].append(a)

for i in range(K):
    s = input()
    a, b = s.split(" ")
    a = int(a) - 1
    b = int(b) - 1
    outlist[a].append(b)
    outlist[b].append(a)

result = []
for i in range(N):
    node = nodes[i]
    ret = node.root.volume - 1
    for out in outlist[i]:
        if nodes[out].rootnum == node.rootnum:
            ret -=1
    result.append(ret)

print(" ".join([str(i) for i in result]))

Buggy submission (verdict: Runtime Error):

N, M, K = [int(i) for i in input().split(" ")]

friend_list = []
block_list = []
union = [None for i in range(N)]
next = 0

for i in range(M):
    s = input()
    a, b = s.split(" ")
    a = int(a) - 1
    b = int(b) - 1

    friend_list.append((a, b))

    if union[a] is union[b] is None:
        union[a] = next
        union[b] = next
        next += 1

    elif union[a] is None:
        union[a] = union[b]
    elif union[b] is None:
        union[b] = union[a]

    elif union[a] > union[b]:
        umax = union[a]
        umin = union[b]

        for j in range(M):
            if union[j] == umax:
                union[j] = umin

    elif union[b] > union[a]:
        umax = union[b]
        umin = union[a]

        for j in range(M):
            if union[j] == umax:
                union[j] = umin


for i in range(K):
    s = input()
    a, b = s.split(" ")
    a = int(a) - 1
    b = int(b) - 1
    block_list.append((a, b))


result = []
for i in range(N):
    ret = 0
    if union[i] is None:
        result.append(0)
        continue
    for j in range(N):
        if i == j:
            continue
        if union[i] != union[j]:
            continue
        if (i, j) in block_list:
            continue
        if (j, i) in block_list:
            continue
        if (i, j) in friend_list:
            continue
        if (j, i) in friend_list:
            continue

        ret += 1
    result.append(ret)

#print(result)
print(" ".join([str(i) for i in result]))

Buggy submission (verdict: Runtime Error):

N, M, K = [int(i) for i in input().split(" ")]

friend_list = []
block_list = []
union = [None for i in range(N)]
next = 0

for i in range(M):
    s = input()
    a, b = s.split(" ")
    a = int(a) - 1
    b = int(b) - 1

    friend_list.append((a, b))

    if union[a] is union[b] is None:
        union[a] = next
        union[b] = next
        next += 1

    elif union[a] is None:
        union[a] = union[b]
    elif union[b] is None:
        union[b] = union[a]

    elif union[a] > union[b]:
        umax = union[a]
        umin = union[b]

        for j in range(M):
            if union[j] == umax:
                union[j] = umin

    elif union[b] > union[a]:
        umax = union[b]
        umin = union[a]

        for j in range(M):
            if union[j] == umax:
                union[j] = umin



for i in range(K):
    s = input()
    a, b = s.split(" ")
    a = int(a) - 1
    b = int(b) - 1
    block_list.append((a, b))

result = []
for i in range(N):
    ret = 0
    for j in range(N):
        if i == j:
            continue

        if union[i] != union[j]:
            continue
        if (i, j) in block_list:
            continue
        if (j, i) in block_list:
            continue
        if (i, j) in friend_list:
            continue
        if (j, i) in friend_list:
            continue

        ret += 1
    result.append(ret)

#print(result)
print(" ".join([str(i) for i in result]))

Buggy submission (verdict: Runtime Error):
N, M, K = [int(i) for i in input().split(" ")]

friend_list = []
block_list = []
union = [None for i in range(N)]
next = 0

for i in range(M):
    s = input()
    a, b = s.split(" ")
    a = int(a) - 1
    b = int(b) - 1

    friend_list.append((a, b))

    if union[a] is union[b] is None:
        union[a] = next
        union[b] = next
        next += 1

    elif union[a] is None:
        union[a] = union[b]
    elif union[b] is None:
        union[b] = union[a]

    elif union[a] > union[b]:
        umax = union[a]
        umin = union[b]

        for j in range(M):
            if union[j] == umax:
                union[j] = umin

    elif union[b] > union[a]:
        umax = union[b]
        umin = union[a]

        for j in range(M):
            if union[j] == umax:
                union[j] = umin



for i in range(K):
    s = input()
    a, b = s.split(" ")
    a = int(a) - 1
    b = int(b) - 1
    block_list.append((a, b))

result = []
for i in range(N):
    ret = 0
    for j in range(N):
        if i == j:
            continue

        if union[i] != union[j]:
            continue
        if (i, j) in block_list:
            continue
        if (j, i) in block_list:
            continue
        if (i, j) in friend_list:
            continue
        if (j, i) in friend_list:
            continue

        ret += 1
    result.append(ret)

#print(result)
print(" ".join([str(i) for i in result]))