Problem p02762 (python)

Fixed submission:
class UnionFind:
    """
    parents: 各要素の親要素
             要素がrootの場合には　-(そのグループに属する要素数) を格納
    """
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        # xが属するグループのrootを取得
        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())

      
n, m, k = map(int, input().split())

union_find = UnionFind(n)
friends_counter = {i: 0 for i in range(n)}
blocks_counter = {i: 0 for i in range(n)}

for _ in range(m):
    a, b = map(int, input().split())
    union_find.union(a-1, b-1)
    friends_counter[a-1] += 1
    friends_counter[b-1] += 1

for _ in range(k):
    c, d = map(int, input().split())
    if union_find.same(c-1, d-1):
        blocks_counter[c-1] += 1
        blocks_counter[d-1] += 1

result = []
for i in range(n):
    result.append(str(union_find.size(i) - friends_counter[i] - blocks_counter[i] - 1))

print(" ".join(result))

Buggy submission (verdict: Runtime Error):
friend_dic = {}
block_dic = {}
results = {}
N, M, K = map(int, input().split())

for i in range(1, N+1):
    friend_dic[i] = []
    block_dic[i] = []
    results[i] = None

for _ in range(M):
    a, b = map(int, input().split())
    friend_dic[a].append(b)
    friend_dic[b].append(a)

for _ in range(K):
    a, b = map(int, input().split())
    block_dic[a].append(b)
    block_dic[b].append(a)

def dfs(graph, start, cluster):
    cluster.append(start)
    for neighbor in graph[start]:
        if neighbor not in cluster:
            dfs(graph, neighbor, cluster)


result = []
for i in range(1, N+1):
    if results[i] is not None:
        result += results[i]
    else:
        cluster = []
        dfs(friend_dic, i, cluster)
        for v in cluster:
            results[v] = str(len(set(cluster) - set(friend_dic[v]) - set(block_dic[v])) - 1)
        result += results[i]
print(" ".join(result))

Buggy submission (verdict: Runtime Error):
friend_dic = {}
block_dic = {}
results = {}

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

for i in range(1, N+1):
    friend_dic[i] = []
    block_dic[i] = []
    results[i] = None

for _ in range(M):
    a, b = map(int, input().split())
    friend_dic[a].append(b)
    friend_dic[b].append(a)

for _ in range(K):
    a, b = map(int, input().split())
    block_dic[a].append(b)
    block_dic[b].append(a)

def dfs(graph, start, cluster):
    cluster.append(start)
    for neighbor in graph[start]:
        if neighbor not in cluster:
            dfs(graph, neighbor, cluster)


result = []
for i in range(1, N+1):
    if results[i] is not None:
        result += results[i]
    else:
        cluster = []
        dfs(friend_dic, i, cluster)
        for v in cluster:
            results[v] = str(len(set(cluster) - set(friend_dic[v]) - set(block_dic[v])) - 1)
        result += results[i]
print(" ".join(result))

Buggy submission (verdict: Runtime Error):
friend_dic = {}
block_dic = {}
clusters = {}

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

for i in range(1, N+1):
    friend_dic[i] = []
    block_dic[i] = []
    clusters[i] = None

for _ in range(M):
    a, b = map(int, input().split())
    friend_dic[a].append(b)
    friend_dic[b].append(a)

for _ in range(K):
    a, b = map(int, input().split())
    block_dic[a].append(b)
    block_dic[b].append(a)

def dfs(graph, start, cluster):
    cluster.append(start)
    for neighbor in graph[start]:
        if neighbor not in cluster:
            dfs(graph, neighbor, cluster)


result = ""
for i in range(1, N+1):
    if clusters[i] is not None:
        cluster = clusters[i]
    else:
        cluster = []
        dfs(friend_dic, i, cluster)
        for v in cluster:
            clusters[v] = cluster
    result += str(len(set(cluster) - set(friend_dic[i]) - set(block_dic[i])) - 1)
print(" ".join(result))

Buggy submission (verdict: Runtime Error):
friend_dic = {}
block_dic = {}
cluster_sizes = {}

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

for i in range(N):
    friend_dic[i+1] = []
    block_dic[i+1] = []
    cluster_sizes[i+1] = 0

for _ in range(M):
    a, b = map(int, input().split())
    friend_dic[a].append(b)
    friend_dic[b].append(a)

for _ in range(K):
    a, b = map(int, input().split())
    block_dic[a].append(b)
    block_dic[b].append(a)

def dfs(graph, start, cluster):
    cluster.append(start)
    for neighbor in graph[start]:
        if neighbor not in cluster:
            dfs(graph, neighbor, cluster)


result = ""
for i in range(1, N+1):
    if cluster_sizes[i] != 0:
        cluster_size = cluster_sizes[i]
    else:
        cluster = []
        dfs(friend_dic, i, cluster)
        cluster_size = len(cluster)
        for v in cluster:
            cluster_sizes[v] = cluster_size
    result += str(cluster_size - len(friend_dic[i]) - len(block_dic[i]) - 1)

print(" ".join(result))

Buggy submission (verdict: Runtime Error):
friend_dic = {}
block_dic = {}
cluster_size = {}

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

for i in range(N):
    friend_dic[i+1] = 0
    block_dic[i+1] = 0
    cluster_size[i+1] = 0

for _ in range(M):
    a, b = map(int, input().split())
    friend_dic[a] += 1
    friend_dic[b] += 1

for _ in range(K):
    a, b = map(int, input().split())
    block_dic[a] += 1
    block_dic[b] += 1

def dfs(graph, start, cluster):
    cluster.append(start)
    for neighbor in graph[start]:
        if neighbor not in cluster:
            dfs(graph, neighbor, cluster)


result = ""
for i in range(1, N+1):
    if cluster_sizes[i] != 0:
        cluster_size = cluster_sizes[i]
    else:
        cluster = []
        dfs(friend_dic, i, cluster)
        cluster_size = len(cluster)
        for v in cluster:
            cluster_sizes[v] = cluster_size
    result += str(cluster_size - friend_dic[i] - block_dic[i] - 1)

print(" ".join(result))

Buggy submission (verdict: Runtime Error):
friend_dic = {}
block_dic = {}

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

for i in range(N):
    friend_dic[i+1] = []
    block_dic[i+1] = []

for _ in range(M):
    a, b = map(int, input().split())
    friend_dic[a].append(b)
    friend_dic[b].append(a)

for _ in range(K):
    a, b = map(int, input().split())
    block_dic[a].append(b)
    block_dic[b].append(a)

def search_friends(n, friends):
    searched.append(n)
    for my_friend in friend_dic[n]:
        if my_friend not in friends:
            friends.append(my_friend)
            search_friends(my_friend, friends)

result = []
clusters = []
searched = []
for i in range(1, N+1):
    friends = []
    if i in searched:
        for cluster in clusters:
            if i in cluster:
                friends_set = cluster
                break
    else:
        search_friends(i, friends)
        friends_set = set(friends)
        clusters.append(friends_set)
    result.append(str(len(friends_set-set(friend_dic[i])-set(block_dic[i])-{i})))

print(" ".join(result))

Buggy submission (verdict: Runtime Error):
friend_dic = {}
block_dic = {}

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

for i in range(N):
    friend_dic[i+1] = []
    block_dic[i+1] = []

for _ in range(M):
    a, b = map(int, input().split())
    friend_dic[a].append(b)
    friend_dic[b].append(a)

for _ in range(K):
    a, b = map(int, input().split())
    block_dic[a].append(b)
    block_dic[b].append(a)

def search_friends(n, friends):
    for my_friend in friend_dic[n]:
        if my_friend not in friends:
            friends.append(my_friend)
            search_friends(my_friend, friends)

result = []
clusters = []
for i in range(1, N+1):
    friends = []
    flag = False
    for cluster in clusters:
        if i in cluster:
            friends_set = cluster
            flag = True
            break
    if not flag:
        search_friends(i, friends)
        friends_set = set(friends)
        clusters.append(friends_set)
    result.append(str(len(friends_set-set(friend_dic[i])-set(block_dic[i])-{i})))

print(" ".join(result))

Buggy submission (verdict: Runtime Error):
friend_dic = {}
block_dic = {}

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

for i in range(N):
    friend_dic[i+1] = []
    block_dic[i+1] = [i+1]

for _ in range(M):
    a, b = map(int, input().split())
    friend_dic[a].append(b)
    friend_dic[b].append(a)

for _ in range(K):
    a, b = map(int, input().split())
    block_dic[a].append(b)
    block_dic[b].append(a)

def search_friends(n, friends):
    for my_friend in friend_dic[n]:
        if my_friend not in friends:
            friends.append(my_friend)
            search_friends(my_friend, friends)

result = []
for i in range(1, N+1):
    friends = []
    search_friends(i, friends)
    result.append(str(len(set(friends)-set(friend_dic[i])-set(block_dic[i]))))

print(" ".join(result))