Problem:


Score : 400 points

Problem Statement

An SNS has N users - User 1, User 2, \cdots, User N.

Between these N users, there are some relationships - M friendships and K blockships.

For each i = 1, 2, \cdots, M, there is a bidirectional friendship between User A_i and User B_i.

For each i = 1, 2, \cdots, K, there is a bidirectional blockship between User C_i and User D_i.

We define User a to be a friend candidate for User b when all of the following four conditions are satisfied:

a \neq b.

There is not a friendship between User a and User b.

There is not a blockship between User a and User b.

There exists a sequence c_0, c_1, c_2, \cdots, c_L consisting of integers between 1 and N (inclusive) such that c_0 = a, c_L = b, and there is a friendship between User c_i and c_{i+1} for each i = 0, 1, \cdots, L - 1.

For each user i = 1, 2, ... N, how many friend candidates does it have?

Constraints

All values in input are integers.

2 ≤ N ≤ 10^5

0 \leq M \leq 10^5

0 \leq K \leq 10^5

1 \leq A_i, B_i \leq N

A_i \neq B_i

1 \leq C_i, D_i \leq N

C_i \neq D_i

(A_i, B_i) \neq (A_j, B_j) (i \neq j)

(A_i, B_i) \neq (B_j, A_j)

(C_i, D_i) \neq (C_j, D_j) (i \neq j)

(C_i, D_i) \neq (D_j, C_j)

(A_i, B_i) \neq (C_j, D_j)

(A_i, B_i) \neq (D_j, C_j)

Input

Input is given from Standard Input in the following format:

N M K
A_1 B_1
\vdots
A_M B_M
C_1 D_1
\vdots
C_K D_K

Output

Print the answers in order, with space in between.

Sample Input 1

4 4 1
2 1
1 3
3 2
3 4
4 1

Sample Output 1

0 1 0 1

There is a friendship between User 2 and 3, and between 3 and 4. Also, there is no friendship or blockship between User 2 and 4. Thus, User 4 is a friend candidate for User 2.

However, neither User 1 or 3 is a friend candidate for User 2, so User 2 has one friend candidate.

Sample Input 2

5 10 0
1 2
1 3
1 4
1 5
3 2
2 4
2 5
4 3
5 3
4 5

Sample Output 2

0 0 0 0 0

Everyone is a friend of everyone else and has no friend candidate.

Sample Input 3

10 9 3
10 1
6 7
8 2
2 5
8 4
7 3
10 9
6 4
5 8
2 6
7 5
3 1

Sample Output 3

1 3 5 4 3 3 3 3 1 0



Sample input:
4 4 1
2 1
1 3
3 2
3 4
4 1


Sample output:
0 1 0 1

Submission status: Runtime Error

Solution code:
mport sys

sys.setrecursionlimit(10 ** 6)

input = sys.stdin.readline

def friends(friend, array, i, label, t):
    for j in range(len(friend[i])):
        if label[friend[i][j] - 1] == 0:
            label[friend[i][j] - 1] = label[i]
            array.append(friend[i][j] - 1)
            return friends(friend, array, friend[i][j] - 1, label, t + 1)
    
    if t != 1:
        array.pop(-1)
        return friends(friend, array, array[t - 1], label, t - 1)
    else:
        return label   
        
n, m, k = map(int, input().split())
a = [0] * m
b = [0] * m
c = [0] * k
d = [0] * k

label = [0] * n
kosu_label = 1

block = [0] * n

ans = [0] * n

for i in range(m):
    a[i], b[i] = map(int, input().split())
    
for i in range(k):
    c[i], d[i] = map(int, input().split())
    
friend = [[] for i in range(n)]
    
for i in range(m):
    friend[a[i] - 1].append(b[i])
    friend[b[i] - 1].append(a[i])

for i in range(n):
    if label[i] == 0:
        label[i] = kosu_label
        label = friends(friend, [-1, i], i, label, 1)
            
    kosu_label = kosu_label + 1

label_s = [0] * kosu_label

for i in range(n):
    label_s[label[i]] = label_s[label[i]] + 1
    
for w in range(k):
    if label[c[w] - 1] == label[d[w] - 1]:
        block[c[w] - 1] = block[c[w] - 1] + 1
        block[d[w] - 1] = block[d[w] - 1] + 1
            
for i in range(n):
    ans[i] = label_s[label[i]] - len(friend[i]) - block[i] - 1
    print(ans[i], end = ' ')

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 5)

def friends(friend, array, i, label, t):
    for j in range(len(friend[i])):
        if label[friend[i][j] - 1] == 0:
            label[friend[i][j] - 1] = label[i]
            array.append(friend[i][j] - 1)
            return friends(friend, array, friend[i][j] - 1, label, t + 1)
    
    if t != 1:
        array.pop(-1)
        return friends(friend, array, array[t - 1], label, t - 1)
    else:
        return label   
        
n, m, k = map(int, input().split())
a = [0] * m
b = [0] * m
c = [0] * k
d = [0] * k

label = [0] * n
kosu_label = 1

block = [0] * n

ans = [0] * n

for i in range(m):
    a[i], b[i] = map(int, input().split())
    
for i in range(k):
    c[i], d[i] = map(int, input().split())
    
friend = [[] for i in range(n)]
    
for i in range(m):
    friend[a[i] - 1].append(b[i])
    friend[b[i] - 1].append(a[i])

for i in range(n):
    if label[i] == 0:
        label[i] = kosu_label
        label = friends(friend, [-1, i], i, label, 1)
            
    kosu_label = kosu_label + 1

label_s = [0] * kosu_label

for i in range(n):
    label_s[label[i]] = label_s[label[i]] + 1
    
for w in range(k):
    if label[c[w] - 1] == label[d[w] - 1]:
        block[c[w] - 1] = block[c[w] - 1] + 1
        block[d[w] - 1] = block[d[w] - 1] + 1
            
for i in range(n):
    ans[i] = label_s[label[i]] - len(friend[i]) - block[i] - 1
    print(ans[i], end = ' ')

Submission status: Runtime Error

Solution code:
def friends(friend, array, i, label, t):
    for j in range(len(friend[i])):
        if label[friend[i][j] - 1] == 0:
            label[friend[i][j] - 1] = label[i]
            array.append(friend[i][j] - 1)
            return friends(friend, array, friend[i][j] - 1, label, t + 1)
    
    if t != 1:
        array.pop(-1)
        return friends(friend, array, array[t - 1], label, t - 1)
    else:
        return label   
        
n, m, k = map(int, input().split())
a = [0] * m
b = [0] * m
c = [0] * k
d = [0] * k

label = [0] * n
kosu_label = 1

block = [0] * n

ans = [0] * n

for i in range(m):
    a[i], b[i] = map(int, input().split())
    
for i in range(k):
    c[i], d[i] = map(int, input().split())
    
friend = [[] for i in range(n)]
    
for i in range(m):
    friend[a[i] - 1].append(b[i])
    friend[b[i] - 1].append(a[i])

for i in range(n):
    if label[i] == 0:
        label[i] = kosu_label
        label = friends(friend, [-1, i], i, label, 1)
            
    kosu_label = kosu_label + 1

label_s = [0] * kosu_label

for i in range(n):
    label_s[label[i]] = label_s[label[i]] + 1
    
for w in range(k):
    if label[c[w] - 1] == label[d[w] - 1]:
        block[c[w] - 1] = block[c[w] - 1] + 1
        block[d[w] - 1] = block[d[w] - 1] + 1
            
for i in range(n):
    ans[i] = label_s[label[i]] - len(friend[i]) - block[i] - 1

for i in range(n):
    print(ans[i], end = ' ')

Submission status: Runtime Error

Solution code:
def friends(friend, array, i, label, t):
    for j in range(len(friend[i])):
        if label[friend[i][j] - 1] == 0:
            label[friend[i][j] - 1] = label[i]
            array.append(friend[i][j] - 1)
            return friends(friend, array, friend[i][j] - 1, label, t + 1)
    
    if t != 1:
        array.pop(-1)
        return friends(friend, array, array[t - 1], label, t - 1)
    else:
        return label   
        


n, m, k = map(int, input().split())
a = [0] * m
b = [0] * m
c = [0] * k
d = [0] * k

label = [0] * n
kosu_label = 1

block = [0] * n

ans = [0] * n

for i in range(m):
    a[i], b[i] = map(int, input().split())
    
for i in range(k):
    c[i], d[i] = map(int, input().split())
    
friend = [[] for i in range(n)]
    
for i in range(m):
    friend[a[i] - 1].append(b[i])
    friend[b[i] - 1].append(a[i])

for i in range(n):
    if label[i] == 0:
        label[i] = kosu_label
        label = friends(friend, [-1, i], i, label, 1)
            
    kosu_label = kosu_label + 1

label_s = [0] * kosu_label

for i in range(n):
    label_s[label[i]] = label_s[label[i]] + 1
    
for w in range(k):
    if label[c[w] - 1] == label[d[w] - 1]:
        block[c[w] - 1] = block[c[w] - 1] + 1
        block[d[w] - 1] = block[d[w] - 1] + 1
            
for i in range(n):
    ans[i] = label_s[label[i]] - len(friend[i]) - block[i] - 1

for i in range(n):
    print(ans[i], end = ' ')

Submission status: Runtime Error

Solution code:
class UnionFind:
    def __init__(self, n) -> None:
        self.n = n
        self.parents = [-1] * (n+1)
    
    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
        
        self.parents[x] += self.parents[y]
        self.parents[y] = x
    
    def issame(self, x, y):
        return self.find(x) == self.find(y)
    
    def count(self, x):
        return -self.parents[self.find(x)]
    
    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 __str__(self):
        return '\n'.join('{}: {}'.format(root, self.members(root)) for root in self.roots())

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

uf_friend = UnionFind(n+1)
count = [0] * (n+1)
for i in range(m):
    a, b = map(int, input().split(' '))
    uf_friend.union(a, b)
    count[a] += 1
    count[b] += 1

for i in range(k):
    a, b = map(int, input().split(' '))
    if uf_friend.issame(a, b):
        count[a] += 1
        count[b] += 1

result = [0] * n
for i in range(1, n+1):
    result[i - 1] = uf_friend.count(i) - count[i] - 1

print(*result)

Submission status: Runtime Error

Solution code:
class UnionFind:
    def __init__(self, n) -> None:
        self.n = n
        self.parents = [-1] * (n+1)
    
    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
        
        self.parents[x] += self.parents[y]
        self.parents[y] = x
    
    def issame(self, x, y):
        return self.find(x) == self.find(y)
    
    def count(self, x):
        return -self.parents[self.find(x)]
    
    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 __str__(self):
        return '\n'.join('{}: {}'.format(root, self.members(root)) for root in self.roots())

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

uf_friend = UnionFind(n+1)
count = [0] * (n+1)
for i in range(m):
    a, b = map(int, input().split(' '))
    uf_friend.union(a, b)
    count[a] += 1
    count[b] += 1

for i in range(k):
    a, b = map(int, input().split(' '))
    if uf_friend.issame(a, b):
        count[a] += 1
        count[b] += 1

result = [0] * n
for i in range(1, n+1):
    return[i - 1] = uf_friend.count(i) - count[i] - 1

print(*result)

Submission status: Runtime Error

Solution code:
def friends(friend, array, i, label, t):
    for j in range(len(friend[i])):
        if label[friend[i][j] - 1] == 0:
            label[friend[i][j] - 1] = label[i]
            array.append(friend[i][j] - 1)
            return friends(friend, array, friend[i][j] - 1, label, t + 1)
    
    if t != 1:
        array.pop(-1)
        return friends(friend, array, array[t - 1], label, t - 1)
    else:
         return label   
        


n, m, k = map(int, input().split())
a = [0] * m
b = [0] * m
c = [0] * k
d = [0] * k

label = [0] * n
kosu_label = 1

block = [0] * n

ans = [0] * n

for i in range(m):
    a[i], b[i] = map(int, input().split())
    
for i in range(k):
    c[i], d[i] = map(int, input().split())
    
friend = [[] for i in range(n)]
    
for i in range(m):
    friend[a[i] - 1].append(b[i])
    friend[b[i] - 1].append(a[i])

for i in range(n):
    if label[i] == 0:
        label[i] = kosu_label
        label = friends(friend, [-1, i], i, label, 1)
            
    kosu_label = kosu_label + 1

label_s = [0] * kosu_label

for i in range(n):
    label_s[label[i]] = label_s[label[i]] + 1
    
for w in range(k):
    if label[c[w] - 1] == label[d[w] - 1]:
        block[c[w] - 1] = block[c[w] - 1] + 1
        block[d[w] - 1] = block[d[w] - 1] + 1
            
for i in range(n):
    ans[i] = label_s[label[i]] - len(friend[i]) - block[i] - 1

for i in range(n):
    print(ans[i], end = ' ')

Submission status: Runtime Error

Solution code:

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]))

Submission status: Runtime Error

Solution code:

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]))

Submission status: Runtime Error

Solution code:
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]))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from collections import defaultdict


def main():
    n, m, k = map(int, input().split())
    fr_ed_c = defaultdict(int)  # node -> count

    parents = {}  # node -> parent_node
    root_2_nc = {}  # node -> count

    for i in range(m):
        a, b = map(int, input().split())
        fr_ed_c[a] += 1
        fr_ed_c[b] += 1

        p_a = get_root(parents, a)
        p_b = get_root(parents, b)

        if p_a == p_b:
            # already same cluster. do nothing.
            pass
        else:
            # cluster integration
            p_a_c = root_2_nc.get(p_a, 1)
            p_b_c = root_2_nc.get(p_b, 1)
            p_a, p_b = (p_a, p_b) if p_a_c < p_b_c else (p_b, p_a)
            parents[p_a] = p_b
            if p_b not in root_2_nc:
                root_2_nc[p_b] = 1
            root_2_nc[p_b] += root_2_nc.pop(p_a, 1)

    br_ed = defaultdict(set)  # node -> set()
    for i in range(k):
        c, d = map(int, input().split())
        br_ed[c].add(d)
        br_ed[d].add(c)

    ans_list = []
    for i_m in range(n):
        i = i_m + 1
        root = get_root(parents, i)
        block_count = 0
        for b in br_ed[i]:
            b_root = get_root(parents, b)
            block_count += 1 if root == b_root else 0
        ans = root_2_nc[root] - 1 - fr_ed_c[i] - block_count
        ans_list.append(str(ans))
    print(" ".join(ans_list))


def get_root(parents, i):
    if i not in parents:
        return i
    return get_root(parents, parents[i])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from collections import defaultdict


def main():
    n, m, k = map(int, input().split())
    fr_ed_c = defaultdict(int)  # node -> count

    parents = {}  # node -> parent_node
    root_2_nc = {}  # node -> count

    for i in range(m):
        a, b = map(int, input().split())
        fr_ed_c[a] += 1
        fr_ed_c[b] += 1

        p_a = get_root(parents, a)
        p_b = get_root(parents, b)

        if p_a == p_b:
            # already same cluster. do nothing.
            pass
        else:
            # cluster integration
            parents[p_a] = p_b
            if p_b not in root_2_nc:
                root_2_nc[p_b] = 1
            root_2_nc[p_b] += root_2_nc.pop(p_a, 1)

    br_ed = defaultdict(set)  # node -> set()
    for i in range(k):
        c, d = map(int, input().split())
        br_ed[c].add(d)
        br_ed[d].add(c)

    ans_list = []
    for i_m in range(n):
        i = i_m + 1
        root = get_root(parents, i)
        block_count = 0
        for b in br_ed[i]:
            b_root = get_root(parents, b)
            block_count += 1 if root == b_root else 0
        ans = root_2_nc[root] - 1 - fr_ed_c[i] - block_count
        ans_list.append(str(ans))
    print(" ".join(ans_list))


def get_root(parents, i):
    if i not in parents:
        return i
    return get_root(parents, parents[i])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from collections import defaultdict


def main():
    n, m, k = map(int, input().split())
    a_b_list = []
    fr_ed_c = defaultdict(int)  # node -> count
    for i in range(m):
        a, b = map(int, input().split())
        a_b_list.append([a, b])
        fr_ed_c[a] += 1
        fr_ed_c[b] += 1
    br_ed = defaultdict(set)  # node -> set()
    for i in range(k):
        c, d = map(int, input().split())
        br_ed[c].add(d)
        br_ed[d].add(c)

    parents = {}  # node -> parent_node
    root_2_nc = {}  # node -> count

    for a_b in a_b_list:
        a, b = sorted(a_b)
        p_a = get_root(parents, a)
        p_b = get_root(parents, b)

        if p_a == p_b:
            # already same cluster. do nothing.
            pass
        else:
            # cluster integration
            parents[p_a] = p_b
            if p_b not in root_2_nc:
                root_2_nc[p_b] = 1
            root_2_nc[p_b] += root_2_nc.pop(p_a, 1)

    ans_list = []
    for i_m in range(n):
        i = i_m + 1
        root = get_root(parents, i)
        block_count = 0
        for b in br_ed[i]:
            b_root = get_root(parents, b)
            block_count += 1 if root == b_root else 0
        ans = root_2_nc[root] - 1 - fr_ed_c[i] - block_count
        ans_list.append(str(ans))
    print(" ".join(ans_list))


def get_root(parents, i):
    if i not in parents:
        return i
    return get_root(parents, parents[i])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from collections import defaultdict


def main():
    n, m, k = map(int, input().split())
    a_b_list = []
    fr_ed = defaultdict(set)  # node -> set()
    for i in range(m):
        a, b = map(int, input().split())
        a_b_list.append([a, b])
        fr_ed[a].add(b)
        fr_ed[b].add(a)
    c_d_list = []
    br_ed = defaultdict(set)  # node -> set()
    for i in range(k):
        c, d = map(int, input().split())
        c_d_list.append(list([c, d]))
        br_ed[c].add(d)
        br_ed[d].add(c)

    parents = {}  # node -> parent_node
    root_2_nc = {}  # node -> count

    for a_b in a_b_list:
        a, b = sorted(a_b)
        p_a = get_root(parents, a)
        p_b = get_root(parents, b)

        if p_a == p_b:
            # already same cluster. do nothing.
            pass
        else:
            # cluster integration
            parents[p_a] = p_b
            if p_b not in root_2_nc:
                root_2_nc[p_b] = 1
            root_2_nc[p_b] += root_2_nc.pop(p_a, 1)

    ans_list = []
    for i_m in range(n):
        i = i_m + 1
        root = get_root(parents, i)
        block_count = 0
        for b in br_ed[i]:
            b_root = get_root(parents, b)
            block_count += 1 if root == b_root else 0
        ans = root_2_nc[root] - 1 - len(fr_ed[i]) - block_count
        ans_list.append(str(ans))
    print(" ".join(ans_list))


def get_root(parents, i):
    if i not in parents:
        return i
    return get_root(parents, parents[i])


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def friends(friend, array, i, label, t):
    for j in range(len(friend[i])):
        if label[friend[i][j] - 1] == 0:
            label[friend[i][j] - 1] = label[i]
            array.append(friend[i][j] - 1)
            return friends(friend, array, friend[i][j] - 1, label, t + 1)
    
    if t != 1:
        return friends(friend, array, array[t - 1], label, t - 1)
    else:
         return label   
        


n, m, k = map(int, input().split())
a = [0] * m
b = [0] * m
c = [0] * k
d = [0] * k

label = [0] * n
kosu_label = 1

block = [0] * n

ans = [0] * n

for i in range(m):
    a[i], b[i] = map(int, input().split())
    
for i in range(k):
    c[i], d[i] = map(int, input().split())
    
friend = [[] for i in range(n)]
    
for i in range(m):
    friend[a[i] - 1].append(b[i])
    friend[b[i] - 1].append(a[i])

for i in range(n):
    friend[i] = sorted(friend[i])

for i in range(n):
    if label[i] == 0:
        label[i] = kosu_label
        label = friends(friend, [-1, i], i, label, 1)
            
    kosu_label = kosu_label + 1

label_s = [0] * kosu_label

for i in range(n):
    label_s[label[i]] = label_s[label[i]] + 1
    
for w in range(k):
    if label[c[w] - 1] == label[d[w] - 1]:
        block[c[w] - 1] = block[c[w] - 1] + 1
        block[d[w] - 1] = block[d[w] - 1] + 1
            
for i in range(n):
    ans[i] = label_s[label[i]] - len(friend[i]) - block[i] - 1

for i in range(n):
    print(ans[i], end = ' ')

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
a = [0] * m
b = [0] * m
c = [0] * k
d = [0] * k

label = [0] * n
kosu_label = 1

block = [0] * n

ans = [0] * n

for i in range(m):
    a[i], b[i] = map(int, input().split())
    
for i in range(k):
    c[i], d[i] = map(int, input().split())
    
friend = [[] for i in range(n)]
    
for i in range(m):
    friend[a[i] - 1].append(b[i])
    friend[b[i] - 1].append(a[i])

for i in range(n):
    if label[i] == 0:
        label[i] = kosu_label
    for j in range(len(friend[i])):
        if label[friend[i][j] - 1] == 0:
            label[friend[i][j] - 1] = label[i]
        elif label[friend[i][j] - 1] < label[i]:
            label[i] = label[friend[i][j] - 1]
        elif label[friend[i][j] - 1] > label[i]:
            label[friend[i][j] - 1] = label[i]
            
    kosu_label = kosu_label + 1  
    
for i in range(n):
    for j in range(len(friend)):
        if label[friend[i][j] - 1] < label[i]:
            label[i] = label[friend[i][j] - 1]

label_s = [0] * kosu_label

for i in range(n):
    label_s[label[i]] = label_s[label[i]] + 1
    
for w in range(k):
    if label[c[w] - 1] == label[d[w] - 1]:
        block[c[w] - 1] = block[c[w] - 1] + 1
        block[d[w] - 1] = block[d[w] - 1] + 1
            
for i in range(n):
    ans[i] = label_s[label[i]] - len(friend[i]) - block[i] - 1

for i in range(n):
    print(ans[i], end = ' ')

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
a = [0] * m
b = [0] * m
c = [0] * k
d = [0] * k

label = [0] * n
kosu_label = 1

block = [0] * n

ans = [0] * n

for i in range(m):
    a[i], b[i] = map(int, input().split())
    
for i in range(k):
    c[i], d[i] = map(int, input().split())
    
friend = [[] for i in range(n)]
    
for i in range(m):
    friend[a[i] - 1].append(b[i])
    friend[b[i] - 1].append(a[i])

for i in range(n):
    if label[i] == 0:
        label[i] = kosu_label
    for j in range(len(friend[i])):
        if label[friend[i][j] - 1] == 0:
            label[friend[i][j] - 1] = label[i]
        elif label[friend[i][j] - 1] != label[i]:
            label[i] = min(label[friend[i][j] - 1], label[i])
            label[friend[i][j] - 1] = min(label[friend[i][j] - 1], label[i])
            
    kosu_label = kosu_label + 1  
    
for i in range(n):
    if label[friend[i][j] - 1] > label[i]:
        label[friend[i][j] - 1] = label[i]

label_s = [0] * kosu_label

for i in range(n):
    label_s[label[i]] = label_s[label[i]] + 1
    
for w in range(k):
    if label[c[w] - 1] == label[d[w] - 1]:
        block[c[w] - 1] = block[c[w] - 1] + 1
        block[d[w] - 1] = block[d[w] - 1] + 1
            
for i in range(n):
    ans[i] = label_s[label[i]] - len(friend[i]) - block[i] - 1

for i in range(n):
    print(ans[i], end = ' ')

Submission status: Runtime Error

Solution code:
def root(x):#親のrootはマイナスのサイズ、子のrootは親のindex
  if par[x]<0:
    return x
  else:
    par[x]=root(par[x])
    return par[x]

def unit(x,y):#xとyをくっつける
  rx=root(x)
  ry=root(y)
  if rx==ry:
    return False
  if par[rx]>par[ry]:#親のサイズの大小比較
    rx,ry=ry,rx#xのサイズのほうが大きくする
  par[rx]+=par[ry]#xのサイズにyのサイズを足す(サイズの変換)
  par[ry]=x#親じゃなくなったroot(y)の値をxの根にする
  return True
 
def same(x,y):
  rx=root(x)
  ry=root(y)
  return rx==ry#真偽を返す
 
def size(x):
  return -par[root(x)]#親のrootはマイナスのサイズなため
 
N,M,K=map(int,input().split())
par=[-1]*N
deg=[0]*N
bro=[[]*N for i in range(N)]
for i in range(M):
  a,b=map(int,input().split())
  a-=1
  b-=1
  deg[a]+=1
  deg[b]+=1
  unit(a,b)
for j in range(K):
  c,d=map(int,input().split())
  c-=1
  d-=1
  bro[c].append(d)
  bro[d].append(c)

for q in range(N):
  ans=size(q)-deg[q]-1
  for w in bro[q]:
    if same(q,w):
      ans-=1
  print(ans,end=" ")

Submission status: Runtime Error

Solution code:
def root(x):#親のrootはマイナスのサイズ、子のrootは親のindex
  if par[x]<0:
    return x
  else:
    return par[x]=root(par[x])

def unit(x,y):#xとyをくっつける
  rx=root(x)
  ry=root(y)
  if rx==ry:
    return False
  if par[rx]>par[ry]:#親のサイズの大小比較
    rx,ry=ry,rx#xのサイズのほうが大きくする
  par[rx]+=par[ry]#xのサイズにyのサイズを足す(サイズの変換)
  par[ry]=x#親じゃなくなったroot(y)の値をxの根にする
  return True

def same(x,y):
  rx=root(x)
  ry=root(y)
  return rx==ry#真偽を返す

def size(x):
  return -par[root(x)]#親のrootはマイナスのサイズなため

N,M,K=map(int,input().split())
par=[-1]*N
deg=[0]*N
bro=[[]*N for i in range(N)]
for i in range(M):
  a,b=map(int,input().split())
  a-=1
  b-=1
  deg[a]+=1
  deg[b]+=1
  unit(a,b)
for i in range(K):
  a,b=map(int,input().split())
  a-=1
  b-=1
  bro[a].append(b)
  bro[b].append(a)

for i in range(N):
  ans=size[i]-deg[i]-1
  for j in bro[i]:
    if same(i,j):
      ans-=1
  print(ans,end=" ")

Submission status: Runtime Error

Solution code:
N,M,K = list(map(int,input().split()))
F = []
C = [0]*N #友達の数を記録。
for i in range(M):
    a,b = list(map(int,input().split()))
    C[a-1] += 1
    C[b-1] += 1
    # print("a,b =",a,b)
    ia,ib = -1,-1
    for i in range(len(F)):
        if a in F[i]:
            ia = i
        if b in F[i]:
            ib = i
    # print(ia,ib)
    if ia == -1 and ib == -1:
        F.append([a,b])
    elif ia != -1 and ib == -1:
        F[ia].append(b)
    elif ia == -1 and ib != -1:
        F[ib].append(a)
    else:
        if ia != ib:
            F[ia] += F[ib]
            F.pop(ib)
# print("Group:",F)
# print("The number of friends:",C)
B = []
for i in range(K):
    a = list(map(int,input().split()))
    B.append(a)
# print("Blocks:",B)

# 友達候補者配列
Y = [0]*N
for i in range(N):#人iについて
    l = 1
    # print(i+1,"人目")
    for g in F:
        if i+1 in g:
            g1 = g
            l = len(g)
    # print(g1)
    # print(l)
    # ブロック中の人間を除く。
    for b in B:
        if i+1 in b:
            # print("block!!")
            if b[b.index(i+1)-1] in g1:
                l -= 1
    Y[i] = l-1 - C[i]

# print(C)
print(" ".join(map(str,Y)))

Submission status: Runtime Error

Solution code:
def dfs(count, Graph, i):
    count[i] = 1

    for j in range(len(Graph[i])):
        now = Graph[i][j]
        if count[now] == 0:
            dfs(count, Graph, now)


N, M, K = map(int, input().split())
friend = [list(map(int, input().split())) for i in range(M)]
block = [list(map(int, input().split())) for i in range(K)]
result = []
FG = [[] for i in range(N + 1)]
BG = [[] for i in range(N + 1)]

for i in range(M):
    a = friend[i][0]
    b = friend[i][1]
    FG[a].append(b)
    FG[b].append(a)

for i in range(K):
    a = block[i][0]
    b = block[i][1]
    BG[a].append(b)
    BG[b].append(a)

for i in range(1, N + 1):
    count = [0 for i in range(N + 1)]
    dfs(count, FG, i)
    for j in range(len(BG[i])):
        now = BG[i][j]
        count[now] = 0
    result.append(sum(count) - len(FG[i]) - 1)

print(*result)