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: Wrong Answer

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

class UnionFind:
    def __init__(self,n):
        self.group=[i for i in range(n)]
        self.size=[1 for i in range(n)]

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

    def unite(self,x,y):
        x=self.find(x)
        y=self.find(y)
        if(x==y):
            pass
        elif(self.sizeof(y) > self.sizeof(x)):
            self.group[x] = self.group[y]
            self.size[y] += self.size[x]
            self.size[x]=0
        else:
            self.group[y] = self.group[x]
            self.size[x] += self.size[y]
            self.size[y]=0

    def sizeof(self,a):
        return self.size[self.find(a)]

    def same(self,x,y):
        return self.find(x)==self.find(y)

uf=UnionFind(n)
friends=[0 for _ in range(n)]
blocks=[0 for _ in range(n)]


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

for i in range(k):
    c,d=map(int,input().split())
    c-=1
    d-=1
    blocks[c]+=1
    blocks[d]+=1

ans=[]
for i in range(n):
    ans.append(uf.sizeof(i) - friends[i] - blocks[i]  - 1)
print(*ans)

Submission status: Wrong Answer

Solution code:
import queue
q = queue.Queue()

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

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

block = [[] for _ in range(n)]
for _ in range(k):
    c, d = map(int, input().split())
    block[c-1].append(d-1)
    block[d-1].append(c-1)
    
c_list = []
for i in range(n):
    counter = 0
    seen = [i]
    for j in friend[i]:
        q.put(j)
        seen.append(j)
    while q.qsize() != 0:
        x = q.get()
        for k in friend[x]:
            if not k in seen and not k in block[i]:
                q.put(k)
                seen.append(k)
                counter += 1
    c_list.append(counter)
    
print(' '.join(map(str, c_list)))

Submission status: Wrong Answer

Solution code:
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

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

    # 併合
    def union(self, x, y):
        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)


n, m, k = list(map(int, input().split()))
a = [0]*(m+1)
b = [0]*(m+1)
c = [0]*(k+1)
d = [0]*(k+1)
union_find = UnionFind(n)
num_friends = [-1]*(n+1)
for i in range(m):
    a[i], b[i] = map(int, input().split())
    num_friends[a[i]] += 1
    num_friends[b[i]] += 1
    union_find.union(a[i], b[i])
for i in range(k):
    c[i], d[i] = map(int, input().split())

# frinds_mat = np.zeros([n, n])
# for i in range(m):
#     # a[i]とb[i]は友達
#     frinds_mat[a[i]-1, b[i]-1] = 1
#     frinds_mat[b[i]-1, a[i]-1] = 1
# num_suggest_friend = [-1]*(n+1)  # 自分自身を引いておくので-1で初期化
num_suggest_friend = num_friends
for i in range(1, n+1):
    # num_suggest_friend[i] -= num_friends[i]
    for j in range(1, n+1):
        if (union_find.same_check(i, j)):
            num_suggest_friend[i] += 1
# print(*num_suggest_friend[1:], sep=' ')


# for i in range(1, n+1):
    # print(*num_suggest_friend[1:], sep=' ')
for c_, d_ in zip(c, d):
    if union_find.same_check(c_, d_):
        num_suggest_friend[c_] -= 1
        num_suggest_friend[d_] -= 1

print(*num_suggest_friend[1:], sep=' ')

Submission status: Wrong Answer

Solution code:
from collections import deque

def dfs(friend, group, number, s):
    d = deque([s])
    
    while d:
        num = d.pop()
        if group[num] != 0:
            continue
        
        group[num] = number
        
        for n in friend[num]:
            d.append(n)
    
    return group
    
def main():
    n, m, k = map(int, input().split())
    
    friend = [[] for _ in range(n)]
    for _ in range(m):
        a, b = map(lambda x: int(x) - 1, input().split())
        friend[a].append(b)
        friend[b].append(a)
    
    block = [[] for _ in range(n)]
    for _ in range(k):
        a, b = map(lambda x: int(x) - 1, input().split())
        block[a].append(b)
        block[b].append(a)
    
    group = [0] * n
    number = 1
    for i in range(n):
        if group[i] != 0:
            continue
        
        group = dfs(friend, group, number, i)
        number += 1
        
    link = []
    for i in range(number + 1):
        link.append(group.count(i))
        
    for i in range(n):
        ans = 0
        ans += link[group[i]]
        ans -= len(friend)
        for b in block[i]:
            if group[i] == group[b]:
                ans -= 1
        
        print(ans, end=' ')
        
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import copy 

N ,M ,K = [int(i) for i in input().split(' ')]
AB = defaultdict(deque)

arrive_size = [-1] * N

for _ in range(M):
    a, b = [int(i) for i in input().split(' ')]
    AB[a].append(b)
    AB[b].append(a)

CD = defaultdict(list)
for _ in range(K):
    c, d = [int(i) for i in input().split(' ')]
    CD[c].append(d)
    CD[d].append(c)

def DFS_size(s, Graph):
    stack = deque([s])
    size = 1
    arrive = [0] * N
    tree = set()
    while stack:
        v = stack[-1]
        arrive[v-1] = 1
        tree.add(v)
        if Graph[v]:
            w = Graph[v].popleft()
            if arrive[w-1] == 0:
                if not w in stack:
                    arrive[w-1] =1
                    stack.append(w)
        else:
            stack.pop()

    return tree

tree_all =[]
tree_size = [None] *N
for i in range(1,N+1):
    if arrive_size[i-1] == -1:
        Graph = copy.deepcopy(AB)
        tree = DFS_size(i,Graph)
        size = len(tree)
        for t in tree:
            tree_size[t-1] = size - 1 - len(AB[t]) - len(CD[t])

print(*tree_size)

Submission status: Wrong Answer

Solution code:
import collections
N, M, K = map(int, input().split())
AB = [tuple(map(int, input().split())) for i in range(M)]

CD = [tuple(map(int, input().split())) for i in range(K)]


class unionFind:
    parent = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]

    def root(self, x):
        if self.parent[x] == x:
            return(x)
        else:
            self.parent[x] = self.root(self.parent[x])
            return(self.parent[x])

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y):
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            return


# F = [set() for i in range(N)]
F = unionFind(N)
f = [set() for i in range(N)]
for a, b in AB:
    a -= 1
    b -= 1
    f[a].add(b)
    f[b].add(a)
    # F.unite(a+1, b+1)

B = [set() for i in range(N)]
for c, d in CD:
    c -= 1
    d -= 1
    B[c].add(d)
    B[d].add(c)

ans = [0]*N
# D = collections.defaultdict(set)
# for i in range(N):
#     D[F.root(i)].add(i)

# # print(f)
# for i in range(N):
#     x = D[F.root(i)]
#     ret = len(x)
#     ret -= 1
#     ret -= len(x & f[i])
#     ret -= len(x & B[i])
#     ans[i] = ret
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
import time
def input(): return sys.stdin.readline().strip()

DEBUG = False

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

friend_relations = [[] for _ in range(N+1)]
block_relations = [[] for _ in range(N+1)]

num_connected_friends = [0] * (N+1)
num_connected_blocks = [0] * (N+1)

for _ in range(M):
    A, B = map(int, input().split())
    friend_relations[A].append(B)
    friend_relations[B].append(A)

for _ in range(K):
    C, D = map(int, input().split())
    block_relations[C].append(D)
    block_relations[D].append(C)

if DEBUG:
    print("friend_relations:")
    for i, row in enumerate(friend_relations):
        print("node: {}, relation: {}".format(i, row))
    print("block_matrix:")
    for i, row in enumerate(block_relations):
        print("node: {}, relation: {}".format(i, row))

count = 0
labels = [-1] * (N+1)
graph_sizes = []
total_iteration = 0
for n in range(1, N+1):
    if DEBUG:
        print("root_node: {}, count: {}".format(n, count))
    if labels[n] == -1:
        queue = deque([n])
        labels[n] = count
        size = 0
        while queue:
            if DEBUG:
                print("queue:{}".format(queue))
            node = queue.popleft()
            size += 1
            if DEBUG:
                print("node:{}".format(node))
            for next_node in friend_relations[node]:
                total_iteration += 1
                if labels[next_node] == -1:
                    labels[next_node] = count
                    queue.append(next_node)
            if DEBUG:
                print("labels: {}".format(labels))
            # time.sleep(0.2)
        count += 1
        graph_sizes.append(size)
if DEBUG:
    print("total_iteration: {}".format(total_iteration))
    print("labels: {}".format(labels))
    print("graph_sizes: {}".format(graph_sizes))

friend_candidate = [0] * (N+1)
for n in range(1, N+1):
    num_block = sum([1 for i in block_relations[n] if labels[n] == labels[i]])
    num_frined = len(friend_relations[n])
    if DEBUG:
        print("node: {}, num_frined: {}, num_block: {}".format(n+1, num_frined, num_block))
    friend_candidate[n] = graph_sizes[labels[n]] - num_frined - num_block - 1

print(*friend_candidate)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
import time
def input(): return sys.stdin.readline().strip()

DEBUG = False

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

friend_matrix = [[0] * N for _ in range(N)]
block_matrix = [[0] * N for _ in range(N)]

num_connected_friends = [0] * N
num_connected_blocks = [0] * N

for _ in range(M):
    A, B = map(int, input().split())
    friend_matrix[A-1][B-1] = 1
    friend_matrix[B-1][A-1] = 1
    num_connected_friends[A-1] += 1
    num_connected_friends[B-1] += 1

for _ in range(K):
    C, D = map(int, input().split())
    block_matrix[C-1][D-1] = 1
    block_matrix[D-1][C-1] = 1

if DEBUG:
    # print("friend_matrix: {}".format(friend_matrix))
    print("friend_matrix:")
    for row in friend_matrix:
        print(row)
    # print("block_matrix: {}".format(block_matrix))
    print("block_matrix:")
    for row in block_matrix:
        print(row)
    print("enter!!")

count = 0
labels = [-1] * N
graph_sizes = []
total_iteration = 0
for n in range(N):
    if DEBUG:
        print("root_node: {}, count: {}".format(n+1, count))
    if labels[n-1] == -1:
        queue = deque([n+1])
        labels[n] = count
        size = 0
        while queue:
            total_iteration += 1
            if DEBUG:
                print("queue:{}".format(queue))
            node = queue.popleft()
            size += 1
            if DEBUG:
                print("node:{}".format(node))
            for i in range(N):
                if labels[i] == -1 and friend_matrix[node-1][i] == 1:
                    labels[i] = count
                    queue.append(i+1)
                if labels[node-1] == labels[i]:
                    num_connected_blocks[node-1] += block_matrix[node-1][i]
                    num_connected_blocks[i] += block_matrix[node-1][i]
            if DEBUG:
                print("labels: {}".format(labels))
            # time.sleep(0.2)
        count += 1
        graph_sizes.append(size)
if DEBUG:
    print("total_iteration: {}".format(total_iteration))
    print("labels: {}".format(labels))
    print("graph_sizes: {}".format(graph_sizes))
    print("num_connected_friends: {}".format(num_connected_friends))
    print("num_connected_blocks: {}".format(num_connected_blocks))

frind_candidate = [0] * N
for n in range(N):
    # num_block = sum(block_matrix[n])
    # num_block = 0
    # for i in range(N):
    #     if block_matrix[n][i] == 1 and labels[n] == labels[i]:
    #         num_block += 1
    num_block = num_connected_blocks[n]
    # num_frined = sum(friend_matrix[n])
    num_frined = num_connected_friends[n]
    if DEBUG:
        print("node: {}, num_frined: {}, num_block: {}".format(n+1, num_frined, num_block))
    frind_candidate[n] = graph_sizes[labels[n]] - num_frined - num_block - 1
frind_candidate = list(map(str, frind_candidate))
# print(" ".join(frind_candidate))
print(*frind_candidate)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
import time
def input(): return sys.stdin.readline().strip()

DEBUG = False

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

friend_matrix = [[0] * N for _ in range(N)]
block_matrix = [[0] * N for _ in range(N)]

num_connected_friends = [0] * N
num_connected_blocks = [0] * N

for _ in range(M):
    A, B = map(int, input().split())
    friend_matrix[A-1][B-1] = 1
    friend_matrix[B-1][A-1] = 1
    num_connected_friends[A-1] += 1
    num_connected_friends[B-1] += 1

for _ in range(K):
    C, D = map(int, input().split())
    block_matrix[C-1][D-1] = 1
    block_matrix[D-1][C-1] = 1

if DEBUG:
    # print("friend_matrix: {}".format(friend_matrix))
    print("friend_matrix:")
    for row in friend_matrix:
        print(row)
    # print("block_matrix: {}".format(block_matrix))
    print("block_matrix:")
    for row in block_matrix:
        print(row)
    print("enter!!")

count = 0
labels = [-1] * N
graph_sizes = []
total_iteration = 0
for n in range(N):
    if DEBUG:
        print("root_node: {}, count: {}".format(n+1, count))
    if labels[n-1] == -1:
        queue = deque([n+1])
        labels[n] = count
        size = 0
        while queue:
            total_iteration += 1
            if DEBUG:
                print("queue:{}".format(queue))
            node = queue.popleft()
            size += 1
            if DEBUG:
                print("node:{}".format(node))
            for i in range(N):
                if labels[i] == -1 and friend_matrix[node-1][i] == 1:
                    labels[i] = count
                    queue.append(i+1)
                if labels[node-1] == labels[i]:
                    num_connected_blocks[node-1] += block_matrix[node-1][i]
            if DEBUG:
                print("labels: {}".format(labels))
            # time.sleep(0.2)
        count += 1
        graph_sizes.append(size)
if DEBUG:
    print("total_iteration: {}".format(total_iteration))
    print("labels: {}".format(labels))
    print("graph_sizes: {}".format(graph_sizes))
    print("num_connected_friends: {}".format(num_connected_friends))
    print("num_connected_blocks: {}".format(num_connected_blocks))

frind_candidate = [0] * N
for n in range(N):
    # num_block = sum(block_matrix[n])
    # num_block = 0
    # for i in range(N):
    #     if block_matrix[n][i] == 1 and labels[n] == labels[i]:
    #         num_block += 1
    num_block = num_connected_blocks[n]
    # num_frined = sum(friend_matrix[n])
    num_frined = num_connected_friends[n]
    if DEBUG:
        print("node: {}, num_frined: {}, num_block: {}".format(n+1, num_frined, num_block))
    frind_candidate[n] = graph_sizes[labels[n]] - num_frined - num_block - 1
frind_candidate = list(map(str, frind_candidate))
# print(" ".join(frind_candidate))
print(*frind_candidate)

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
import time
def input(): return sys.stdin.readline().strip()

DEBUG = False

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

friend_matrix = [[0] * N for _ in range(N)]
block_matrix = [[0] * N for _ in range(N)]

num_frineds = [0] * N
num_blocks = [0] * N

for _ in range(M):
    A, B = map(int, input().split())
    friend_matrix[A-1][B-1] = 1
    friend_matrix[B-1][A-1] = 1
    num_frineds[A-1] += 1
    num_frineds[B-1] += 1

for _ in range(K):
    C, D = map(int, input().split())
    block_matrix[C-1][D-1] = 1
    block_matrix[D-1][C-1] = 1

if DEBUG:
    # print("friend_matrix: {}".format(friend_matrix))
    print("friend_matrix:")
    for row in friend_matrix:
        print(row)
    # print("block_matrix: {}".format(block_matrix))
    print("block_matrix:")
    for row in block_matrix:
        print(row)
    print("enter!!")

count = 0
labels = [-1] * N
graph_sizes = []
for n in range(N):
    if DEBUG:
        print("root_node: {}, count: {}".format(n+1, count))
    if labels[n-1] == -1:
        queue = deque([n+1])
        labels[n] = count
        size = 0
        while queue:
            if DEBUG:
                print("queue:{}".format(queue))
            node = queue.popleft()
            size += 1
            if DEBUG:
                print("node:{}".format(node))
            for i in range(N):
                if labels[i] == -1 and friend_matrix[node-1][i] == 1:
                    labels[i] = count
                    queue.append(i+1)
            if DEBUG:
                print("labels: {}".format(labels))
            # time.sleep(0.2)
        count += 1
        graph_sizes.append(size)

if DEBUG:
    print("labels: {}".format(labels))
    print("graph_sizes: {}".format(graph_sizes))

frind_candidate = [0] * N
for n in range(N):
    # num_block = sum(block_matrix[n])
    num_block = 0
    for i in range(N):
        if block_matrix[n][i] == 1 and labels[n] == labels[i]:
            num_block += 1
    # num_frined = sum(friend_matrix[n])
    num_frined = num_frineds[n]
    if DEBUG:
        print("node: {}, num_frined: {}, num_block: {}".format(n+1, num_frined, num_block))
    frind_candidate[n] = graph_sizes[labels[n]] - num_frined - num_block - 1
frind_candidate = list(map(str, frind_candidate))
print(" ".join(frind_candidate))

Submission status: Wrong Answer

Solution code:
from collections import deque
import sys
def input(): return sys.stdin.readline().strip()

DEBUG = False

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

friend_matrix = [[0] * N for _ in range(N)]
block_matrix = [[0] * N for _ in range(N)]

num_frineds = [0] * N
num_blocks = [0] * N

for _ in range(M):
    A, B = map(int, input().split())
    friend_matrix[A-1][B-1] = 1
    friend_matrix[B-1][A-1] = 1
    num_frineds[A-1] += 1
    num_frineds[B-1] += 1

for _ in range(K):
    C, D = map(int, input().split())
    block_matrix[C-1][D-1] = 1
    block_matrix[D-1][C-1] = 1

if DEBUG:
    # print("friend_matrix: {}".format(friend_matrix))
    print("friend_matrix:")
    for row in friend_matrix:
        print(row)
    # print("block_matrix: {}".format(block_matrix))
    print("block_matrix:")
    for row in block_matrix:
        print(row)
    print("enter!!")

count = 0
labels = [-1] * N
graph_sizes = []
for n in range(N):
    size = 0
    if labels[n-1] == -1:
        queue = deque([n])
        while queue:
            node = queue.popleft()
            if labels[node-1] == -1:
                labels[node-1] = count
                size += 1
            else:
                continue
            for i in range(N):
                if labels[i-1] == -1 and friend_matrix[node-1][i-1] == 1:
                    queue.append(i)
        count += 1
        graph_sizes.append(size)
print(num_blocks)
if DEBUG:
    print("labels: {}".format(labels))
    print("graph_sizes: {}".format(graph_sizes))

frind_candidate = [0] * N
for n in range(N):
    # num_block = sum(block_matrix[n])
    num_block = 0
    for i in range(N):
        if block_matrix[n][i] == 1 and labels[n] == labels[i]:
            num_block += 1
    # num_frined = sum(friend_matrix[n])
    num_frined = num_frineds[n]
    if not DEBUG:
        print("node: {}, num_frined: {}, num_block: {}".format(n+1, num_frined, num_block))
    frind_candidate[n] = graph_sizes[labels[n]] - num_frined - num_block - 1
frind_candidate = list(map(str, frind_candidate))
print(" ".join(frind_candidate))

Submission status: Wrong Answer

Solution code:
N,M,K = map(int,input().split())
AB = [list(map(int,input().split())) for i in range(M)]
CD = [list(map(int,input().split())) for i in range(K)]

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

uf = UnionFind(N)
for i in range(M):
    uf.union(AB[i][0]-1,AB[i][1]-1)
pot = [uf.size(i)-1 for i in range(N)]
for i in range(M):
    A,B = AB[i][0],AB[i][1]
    pot[A-1] -= 1
    pot[B-1] -= 1
for i in range(K):
    C,D = CD[i][0],CD[i][1]
    pot[C-1] -= 1
    pot[D-1] -= 1
print(*pot)

Submission status: Wrong Answer

Solution code:
class UnionFind:
  def __init__(self, size):
    self.parents = [x for x in range(size)]
    self.sizes = [1] * size
  
  def unite(self, a, b):
    if self.find(a) == self.find(b):
      return
    
    if self.size_of(a) > self.size_of(b):
      self.sizes[self.find(a)] += self.size_of(b)
      self.parents[self.find(b)] = self.find(a)
    else:
      self.sizes[self.find(b)] += self.size_of(a)
      self.parents[self.find(a)] = self.find(b)
  
  def is_same(self, a, b):
    return self.find(a) == self.find(b)
  
  def size_of(self, a):
    return self.sizes[self.find(a)]
  
  def find(self, a):
    if a == self.parents[a]:
      return a
    else:
      self.parents[a] = self.find(self.parents[a])
      return self.parents[a]


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

u = UnionFind(N)

# 自分自身・友達・ブロック関係のいずれかの場合はフラグ立つ
relations = [1 << i for i in range(N)]

for _ in range(M):
  a, b = map(int, input().split())
  a, b = a - 1, b - 1
  relations[a] |= 1 << b
  relations[b] |= 1 << a
  u.unite(a, b)
  
for _ in range(K):
  c, d = map(int, input().split())
  c, d = c - 1, d - 1
  
  # 同じ連結成分に含まれないブロック関係は無視してok
  if u.is_same(c, d):
    relations[c] |= 1 << d
    relations[d] |= 1 << c

candidates = []

for i in range(N):
  candidates.append(u.size_of(i))
    
print(' '.join(str(x) for x in candidates))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque

# ここにいたるまで結構かかる
# https://note.nkmk.me/python-union-find/
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
        
        # 根のparentsはそのグループの要素数 * (-1) がはいる
        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)]

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

dame = [set() for _ in range(N)]
uf = UnionFind(N)

for _ in range(M):
    a, b = list(map(int, input().split()))
    a -= 1
    b -= 1
    uf.union(a, b)
    dame[a].add(b)
    dame[b].add(a)

for _ in range(K):
    c, d = list(map(int, input().split()))
    c -= 1
    d -= 1
    if uf.find(c) == uf.find(d):
        dame[c].add(d)
        dame[d].add(c)

ans = []
for i in range(N):
    ans.append(uf.size(i) - len(dame[i]) -1 )

print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k=map(int,input().split())
n=int(n)
m=int(m)
k=int(k)
people=[[0 for i in range(n)]for j in range(n)]
for i in range(n):
  people[i][i]=1
for i in range(m):
  a,b=map(int,input().split())
  a=int(a)-1
  b=int(b)-1
  people[a][b]=1
  people[b][a]=1
for i in range(k):
  a,b=map(int,input().split())
  a=int(a)-1
  b=int(b)-1
  people[a][b]=-1
  people[b][a]=-1

candidate=[0 for i in range(n)]
for i in range(n):
  count=0
  for j in range(n):
    if people[i][j]==0:
      count+=1
  candidate[i]=count

for i in candidate:
  print(i,end=' ')
else:
  print()

Submission status: Wrong Answer

Solution code:
n,m,k=map(int,input().split())
n=int(n)
m=int(m)
k=int(k)
people=[[0 for i in range(n)]for j in range(n)]
for i in range(n):
  people[i][i]=1
for i in range(m):
  a,b=map(int,input().split())
  a=int(a)-1
  b=int(b)-1
  people[a][b]=1
  people[b][a]=1
for i in range(k):
  a,b=map(int,input().split())
  a=int(a)-1
  b=int(b)-1
  people[a][b]=-1
  people[b][a]=-1

candidate=[0 for i in range(n)]
for i in range(n):
  count=0
  for j in range(n):
    if people[i][j]==0:
      count+=1
  candidate[i]=count

print(candidate)

Submission status: Wrong Answer

Solution code:
from collections import deque,Counter
n,m,k = map(int,input().split())
G = [[] for _ in range(n)]

exc = [0]*n
for _ in range(m):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    G[a].append(b)
    G[b].append(a)
    exc[a] += 1
    exc[b] += 1

stk = deque()
grp = [-1]*n
grpnum = 0
def dfs(s,g):
    stk.append(s)
    while stk:
        cur = stk.pop()
        for nxt in G[cur]:
            if grp[nxt] != -1: continue
            grp[nxt] = g
            stk.append(nxt)

for i in range(n):
    if grp[i] != -1: continue
    dfs(i,grpnum)
    grpnum += 1
cnt = Counter(grp)

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

ans = [cnt[grp[i]] - exc[i] - 1 for i in range(n)]
print(*ans)

Submission status: Wrong Answer

Solution code:
from collections import deque, defaultdict
import sys
input = sys.stdin.buffer.readline

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

#先ずは連結成分の数を調べる
G = defaultdict(lambda:deque([]))
for _ in range(m):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    G[a].append(b)
    G[b].append(a)

NIL = -1
color = [NIL]*n
def dfs(r, c):
    S = deque([])
    S.append(r)
    color[r] = c
    while S:
        u = S.popleft()
        for v in G[u]:
            if color[v] == NIL:
                color[v] = c
                S.append(v)
                
def make_color():
    id = 1
    for u in range(n):
        if color[u] == NIL:
            dfs(u, id)
            id += 1
            
#グラフの塗り分け完了

#各色ごとの連結成分の数を数える
count = defaultdict(lambda:0)
for i in range(n):
    count[i] = 0
for i in range(n):
    count[color[i]] += 1

#ブロック関係の辞書を作成
block = defaultdict(lambda:deque([]))
for _ in range(k):
    c, d = map(int, input().split())
    c, d = c-1, d-1
    block[c].append(d)
    block[d].append(c)

temp = []
for i in range(n):
    x = count[color[i]] - 1#自分以外の連結成分の和
    y =  len(G[i])#友達の数
    z = 0 #連結内のブロック数
    for v in block[i]:
        if color[i] == color[v]:
            z += 1
    temp.append(x-y-z)
print(*temp)

Submission status: Wrong Answer

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


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

for i in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    union_find.union(a, b)

for i in range(K):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    union_find.same(a, b)

print(union_find)