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

Solution (Python):
n,q,qq=map(int,input().split())


root = list(range(n))

rank =  [0] * n

node =  [1] *n
minus_node =  [0] *n


def find_set(x):
    if(x != root[x]):
        root[x] = find_set(root[x])
    return root[x]

def unite(x,y):
    link(find_set(x),find_set(y))

def link(x,y):
    if root[x] == root[y]:return
    if rank[x] > rank[y]:
        root[y] =x
        node[x] += node[y] 
    else:
        root[x] = y
        node[y] += node[x] 
        if rank[x] == rank[y]:
            rank[y] +=1

def same(x,y):
    return find_set(x) == find_set(y)






for _ in range(q):
    a,b = map(int,input().split())

    unite(a-1,b-1)
    minus_node[a-1] +=1
    minus_node[b-1] +=1
    
    
    
    

for _ in range(qq):
    a,b = map(int,input().split())

    if same(a-1,b-1):
        minus_node[a-1] +=1
        minus_node[b-1] +=1






print(" ".join([str(node[find_set(i)] - minus_node[i] -1) for i in range(n)]))

Solution (Python):



def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]

def unite(x,y):
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        
        if par[x] > par[y]:
            x,y = y,x
        par[x] += par[y]
        par[y] = x
        return True


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


def size(x):
    return -par[find(x)]


n,m,k = [int(i) for i in input().split()]
par = [-1]*n
ans = [-1]*n

import sys
input = sys.stdin.readline

for _ in range(m):
  a,b = [int(j) for j in input().split()]
  unite(a-1,b-1)
  ans[a-1] -= 1
  ans[b-1] -= 1

for _ in range(k):
  c,d = [int(i) for i in input().split()]
  if same(c-1,d-1):
    ans[c-1] -= 1
    ans[d-1] -= 1
    
for i in range(n):
  ans[i] += size(i)
  
ans = list(map(str,ans))
print(' '.join(ans))

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



class UF(object):
    def __init__(self,n):
        self.par = [i for i in range(n)]
        self.rank = [0]*n
        self.size = [1]*n

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

    
    def union(self,x,y):
        px = self.find(x)
        py = self.find(y)
        if px == py:
            return
        else:
            if self.rank[px] < self.rank[py]:
                px, py = py, px
            if self.rank[px] == self.rank[py]:
                self.rank[px] += 1
            self.par[py] = px
            self.size[px] += self.size[py]
            return

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

    
    def get_size(self,x):
        return self.size[self.find(x)]

N,M,K = LI()
A,B = LIR(M,2)
C,D = LIR(K,2)
 
b = defaultdict(set)
t = defaultdict(set)
 
uf = UF(N)
 
for i in range(M):
    uf.union(A[i]-1,B[i]-1)
    t[A[i]-1].add(B[i]-1)
    t[B[i]-1].add(A[i]-1)
    
num = [0]*N
for i in range(K):
    if uf.is_same(C[i]-1, D[i]-1):
        num[C[i]-1] -= 1
        num[D[i]-1] -= 1
        
ans = []
for i in range(N):
    ans.append(uf.get_size(i)-1-len(t[i])+num[i])
print(*ans)

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



class UF(object):
    def __init__(self,n):
        self.par = [i for i in range(n)]
        self.rank = [0]*n
        self.size = [1]*n

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

    
    def union(self,x,y):
        px = self.find(x)
        py = self.find(y)
        if px != py:
            if self.rank[px] < self.rank[py]:
                px, py = py, px
            if self.rank[px] == self.rank[py]:
                self.rank[px] += 1
            self.par[py] = px
            self.size[px] += self.size[py]

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

    
    def get_size(self,x):
        return self.size[self.find(x)]

N,M,K = LI()
A,B = LIR(M,2)
C,D = LIR(K,2)
 
b = defaultdict(set)
t = defaultdict(set)
 
uf = UF(N)
 
for i in range(M):
    uf.union(A[i]-1,B[i]-1)
    t[A[i]-1].add(B[i]-1)
    t[B[i]-1].add(A[i]-1)
    
num = [0]*N
for i in range(K):
    if uf.is_same(C[i]-1, D[i]-1):
        num[C[i]-1] -= 1
        num[D[i]-1] -= 1
        
ans = []
for i in range(N):
    ans.append(uf.get_size(i)-1-len(t[i])+num[i])
print(*ans)

Solution (Python):


import sys

class Node:
    def __init__(self, id):
        self.id = id
        self.label = None
        self.friends = []
        self.blocks = []

sys.setrecursionlimit(10**9)
N,M,K = [int(i) for i in input().split()]

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

for _ in range(M):
    a,b = [int(i) for i in input().split()]
    nodes[a-1].friends.append(b-1)
    nodes[b-1].friends.append(a-1)

for _ in range(K):
    c,d = [int(i) for i in input().split()]
    nodes[c-1].blocks.append(d-1)
    nodes[d-1].blocks.append(c-1)

def dfs(nodes, i, label):
    if nodes[i].label is None:
        nodes[i].label = label
        for j in nodes[i].friends:
            dfs(nodes, j, label)
        
        
        

label = 0
for i in range(len(nodes)):
    dfs(nodes, i, label)
    label+=1

node_cnts = [0] * (label+1)
for i in range(len(nodes)):
    node_cnts[nodes[i].label] += 1
            

result = [0] * len(nodes)
for node in nodes:
    
    b_cnt = 0
    for b in node.blocks:
        if nodes[b].label == node.label:
            b_cnt += 1
            
    result[node.id] = node_cnts[node.label] - len(node.friends) - b_cnt - 1

print(*result)

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

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)
from collections import defaultdict
dic = defaultdict(int)


for _ in range(m):
    a,b = map(int,input().split())
    uf.union(a-1, b-1)
    dic[a-1] += 1
    dic[b-1] += 1
for _ in range(k):
    c,d = map(int,input().split())
    if uf.same(c-1, d-1):
        dic[c-1] += 1
        dic[d-1] += 1

for i in range(n):
    print(uf.size(i) - dic[i]-1)

Solution (Python):
import copy
from collections import deque,Counter
N,M,K=map(int,input().split())
friend=[[] for _ in range(N)]
block=[[] for _ in range(N)]
for i in range(M):                  
    a,b=map(int,input().split())
    friend[a-1].append(b-1)         
    friend[b - 1].append(a - 1)
for i in range(K):
    a,b=map(int,input().split())
    block[a-1].append(b-1)          
    block[b - 1].append(a - 1)      






groupid=[0]*N
l=[]
gone=[]
id=1
v=0
v1=0
v2=0
seen=[0]*N
jointelement=[0]*N
jointnum=0

for i in range(N):
    id=i+1
    d=deque()
    if seen[i]==0:
        d.append(i)
    while len(d)>0:
        v=d.popleft()
        seen[v]=1
        groupid[v]=id
        
        for i in friend[v]:
            if (seen[i]==0)and(groupid[i]==0):
                d.append(i)
        
c = Counter(groupid)
values = list(c.values())  
key = list(c.keys())  
for i in range(len(key)):
    l.append([key[i], values[i]])  

ans=[0]*N
for i in range(N):
    jointnum=c[groupid[i]]
    for j in block[i]:
        if groupid[i]==groupid[j]:
            jointnum=jointnum-1
    ans[i]=jointnum-len(friend[i])-1
print(*ans)






"""なんだこの
while len(f)>0:
    if len(d)==0:
        for i in range(len(gone)):
            groupnum[gone[i]-1]=max(len(gone),groupnum[gone[i]-1])
        gone=[]
        d.append(f[0][0])
    i=0
    v = d.popleft()
    gone.append(v)
    
    while i<len(f):
        if (f[i][0]==v)and(gone.count(f[i][1])==0)and(d.count(f[i][1])==0):
            d.append(f[i][1])
            f.pop(i)
            i=i-1
        elif (f[i][1]==v)and(gone.count(f[i][0])==0)and(d.count(f[i][0])==0):
            d.append(f[i][0])
            f.pop(i)
            i=i-1
        i=i+1
        
        
print(groupnum)
for i in range(M):
    groupnum[f2[i][0]-1]=groupnum[f2[i][0]-1]-1
    groupnum[f2[i][1] - 1] = groupnum[f2[i][1] - 1] - 1
for i in range(K):
    groupnum[b2[i][0]-1]=groupnum[b2[i][0]-1]-1
    groupnum[b2[i][1] - 1] = groupnum[b2[i][1] - 1] - 1
for i in range(N):
    groupnum[i]=groupnum[i]-1
print(*groupnum, sep=" ")


for i in range(M):
    v1=f[i][0]
    v2=f[i][1]
    if (groupid[v1-1]==0)and(groupid[])
"""

Solution (Python):

def main():
    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()}

    import sys
    sys.setrecursionlimit(10**6)
    input = sys.stdin.readline
    n,m,k = map(int,input().split())
    uff = UnionFind(n)
    fr = [0]*n

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


    ans = [uff.size(i) -fr[i]-1 for i in range(n)]

    for i in range(k):
        c,d = map(int,input().split())
        if uff.same(c-1,d-1):
            ans[c-1] -=1
            ans[d-1] -=1
    print(*ans)
if __name__=="__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5 + 1)

class UnionFind:
    def __init__(self, n):
        '木の初期化をする'
        self.p = [-1] * n
        self.rank = [1]*n
        self.size = [1]*n
    def find(self, x):
        'x の親を返す'
        if self.p[x] == -1:
            return x
        else:
            self.p[x] = self.find(self.p[x])
            return self.p[x]

    def unite(self, x, y):
        'rankの低い親を高い方のの親にする'
        if not self.same(x,y):
            x = self.find(x)
            y = self.find(y)
            if self.rank[x] > self.rank[y]:
                x,y = y,x
            elif self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.size[y] += self.size[x]
            self.p[x] = y
            return True
        else:
            return False

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


n,m,k = map(int,input().split())
uf = UnionFind(n)
ans = [0] * n
for i in range(m):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    uf.unite(a,b)
    ans[a] -= 1
    ans[b] -= 1

for i in range(k):
    c,d = map(int,input().split())
    c -= 1
    d -= 1
    if uf.same(c,d):
        ans[c] -= 1
        ans[d] -= 1

for i in range(n):
    p = uf.find(i)
    ans[i] += uf.size[p] - 1

print(' '.join([str(i) for i in ans]))

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

    n,m,k = map(int, input().split())
    
    def find(par, x):
        if par[x] < 0:
            return x
        else:
            par[x] = find(par, par[x])
            return par[x]
    
    def unite(par, x,y):
        x = find(par,x)
        y = find(par,y)
        if x == y:
            return False
        else:
            if par[x] > par[y]:
                x,y = y,x
            par[x] += par[y]
            par[y] = x
            return True
    
    def same(par, x,y):
        return find(par, x) == find(par, y)
    
    def size(par, x):
        return -par[find(par, x)]

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

    res = []
    for i in range(n):
        p = size(friend, i) - 1 -adj[i]
        res.append(p)

    for _ in range(k):
        a,b = map(int, input().split())
        if same(friend, a-1, b-1):
            res[a-1] -= 1
            res[b-1] -= 1

    print(*res)

if __name__ == '__main__':
    main()

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

    n,m,k = map(int, input().split())
    
    def find(par, x):
        if par[x] < 0:
            return x
        else:
            par[x] = find(par, par[x])
            return par[x]
    
    def unite(par, x,y):
        x = find(par,x)
        y = find(par,y)
        if x == y:
            return False
        else:
            if par[x] > par[y]:
                x,y = y,x
            par[x] += par[y]
            par[y] = x
            return True
    
    def same(par, x,y):
        return find(par, x) == find(par, y)
    
    def size(par, x):
        return -par[find(par, x)]

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

    res = []
    for i in range(n):
        p = size(friend, i) - 1 -adj[i]
        res.append(p)

    for _ in range(k):
        a,b = map(int, input().split())
        if same(friend, a-1, b-1):
            res[a-1] -= 1
            res[b-1] -= 1

    print(*res)

if __name__ == '__main__':
    main()

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

    n,m,k = map(int, input().split())
    
    def find(par, x):
        if par[x] < 0:
            return x
        else:
            par[x] = find(par, par[x])
            return par[x]
    
    def unite(par, x,y):
        x = find(par,x)
        y = find(par,y)
        if x == y:
            return False
        else:
            if par[x] > par[y]:
                x,y = y,x
            par[x] += par[y]
            par[y] = x
            return True
    
    def same(par, x,y):
        return find(par, x) == find(par, y)
    
    def size(par, x):
        return -par[find(par, x)]

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

    res = []
    for i in range(n):
        p = size(friend, i) - 1 -len(adj[i])
        res.append(p)

    for _ in range(k):
        a,b = map(int, input().split())
        if same(friend, a-1, b-1):
            res[a-1] -= 1
            res[b-1] -= 1

    print(*res)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

    n,m,k = map(int, input().split())
    
    def find(par, x):
        if par[x] < 0:
            return x
        else:
            par[x] = find(par, par[x])
            return par[x]
    
    def unite(par, x,y):
        x = find(par,x)
        y = find(par,y)
        if x == y:
            return False
        else:
            if par[x] > par[y]:
                x,y = y,x
            par[x] += par[y]
            par[y] = x
            return True
    
    def same(par, x,y):
        return find(par, x) == find(par, y)
    
    def size(par, x):
        return -par[find(par, x)]

    def members(par, x, n):
        root = find(par, x)
        return [i for i in range(n) if find(par, i) == root]
    
    friend = [-1]*n
    adj = [[] for _ in range(n)]
    for _ in range(m):
        a,b = map(int, input().split())
        adj[a-1].append(b-1)
        adj[b-1].append(a-1)
        unite(friend, a-1, b-1)

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

    res = []
    for i in range(n):
        p = size(friend, i) - 1 -len(adj[i])
        for q in block[i]:
            if same(friend, i, q):
                p -= 1
        res.append(p)
    print(*res)

if __name__ == '__main__':
    main()

Solution (Python):
class UnionFind:
    def __init__(self, n):
        self.p = [-1]*n
        
        self.r = [1]*n
 
    def find(self, x):
        if self.p[x] < 0:
            return x
        else:
            self.p[x] = self.find(self.p[x])
            return self.p[x]
 
    def union(self, x, y):
        rx, ry = self.find(x), self.find(y)
 
        if rx != ry:
            if self.r[rx] > self.r[ry]:
                rx, ry = ry, rx
            if self.r[rx] == self.r[ry]:
                self.r[ry] += 1
            self.p[ry] += self.p[rx]
            self.p[rx] = ry
 
    def same(self, x, y):
        return self.find(x) == self.find(y)
 
    def count_member(self, x):
        return -self.p[self.find(x)]

n,m,k=map(int,input().split())
uf=UnionFind(n)
ab,cd=[],[]
for i in range(m):
    a,b=map(int,input().split())
    a-=1
    b-=1
    ab.append((a,b))
    uf.union(a,b)

for i in range(k):
    c,d=map(int,input().split())
    c-=1
    d-=1
    cd.append((c,d))

ans=[]
for i in range(n):
    t=uf.count_member(i)-1
    ans.append(t)

for a,b in ab:
    if uf.same(a,b):
        ans[a]-=1
        ans[b]-=1

for c,d in cd:
    if uf.same(c,d):
        ans[c]-=1
        ans[d]-=1
print(*ans)

Solution (Python):
import collections


def dfs(graph, start):
    visited = set()
    stack = [start]
 
    while stack:
        v = stack.pop()
        if v not in visited:
            stack.extend(graph[v])
            visited.add(v)
            yield v

def read_graph(nodes, edges):
    graph = [set() for _ in range(nodes)]
    for _ in range(edges):
        a, b = (int(x) - 1 for x in input().split())
        graph[a].add(b)
        graph[b].add(a)
 
    return graph

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

friends = read_graph(N, M)
blockings = read_graph(N, K)

candidates = [None for _ in range(N)]
for i in range(N):
    if candidates[i] is not None:
        continue

    s = set()
    for j in dfs(friends, i):
        s.add(j)
        candidates[j] = s

def num_suggestions(i):
    return len(candidates[i]) - len(friends[i]) - len(blockings[i].intersection(candidates[i])) - 1

print(
    " ".join(
        str(num_suggestions(i)) for i in range(N)
    )
)

Solution (Python):
def dfs(graph, start):
    visited = set()
    stack = [start]

    while stack:
        v = stack.pop()
        if v not in visited:
            stack.extend(graph[v])
            visited.add(v)
            yield v


def read_graph(nodes, edges):
    graph = [[] for _ in range(nodes)]
    for _ in range(edges):
        a, b = (int(x) - 1 for x in input().split())
        graph[a].append(b)
        graph[b].append(a)

    return graph


def count_candidates(n, counts, groups, block, friend):
    for i in range(n):
        count = counts[groups[i]]
        for v in block[i]:
            if groups[i] == groups[v]:
                count -= 1

        count -= len(friend[i])
        count -= 1
        yield count


def main():
    n, m, k = map(int, input().split())
    friend = read_graph(n, m)
    block = read_graph(n, k)

    groups = [-1 for _ in range(n)]
    counts = []
    i = 0
    group_id = 0
    while i < n:
        if groups[i] != -1:
            i += 1
            continue

        for j, v in enumerate(dfs(friend, i)):
            groups[v] = group_id

        counts.append(j + 1)
        group_id += 1

    print(" ".join(str(i) for i in count_candidates(n, counts, groups, block, friend)))


if __name__ == "__main__":
    main()

Solution (Python):
class UnionFind:
    def __init__(self, node):
        self.parent = [-1 for _ in range(node)]

    def find(self, target):
        if self.parent[target] < 0:
            return target
        else:
            self.parent[target] = self.find(self.parent[target])
            return self.parent[target]

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

    def union(self, x, y):
        root_x = self.find(x)
        root_y = self.find(y)
        if root_x == root_y:
            return
        if self.parent[root_x] > self.parent[root_y]:
            root_x, root_y = root_y, root_x
        self.parent[root_x] += self.parent[root_y]
        self.parent[root_y] = root_x

    def get_size(self, x):
        return -self.parent[self.find(x)]


def main():
    users, friend_pairs, block_pairs = map(int, input().split())
    answer = [0] * users
    user_relationship = UnionFind(users)
    friend = [[] for _ in range(users)]
    for _ in range(friend_pairs):
        a, b = map(lambda x: int(x) - 1, input().split())
        user_relationship.union(a, b)
        friend[a].append(b)
        friend[b].append(a)
    for _ in range(block_pairs):
        c, d = map(lambda x: int(x) - 1, input().split())
        if user_relationship.is_same(c, d):
            answer[c] -= 1
            answer[d] -= 1
    for i in range(users):
        answer[i] += user_relationship.get_size(i) - 1 - len(friend[i])
    print(" ".join(map(str, answer)))


if __name__ == '__main__':
    main()

Solution (Python):


class UnionFind:
    def __init__(self, n):
        self.parents = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.length = [1 for _ in range(n)] 

    def makeSet(self, n):
        self.parents = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.length = [1 for _ in range(n)]

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

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] < self.rank[y]:
            self.parents[x] = y
            self.length[y] += self.length[x]
        else:
            self.parents[y] = x
            self.length[x] += self.length[y]
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def getLength(self, x):
        x = self.find(x)
        return self.length[x]        

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

def main():

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

    to = [[] for _ in range(n)]
    for _ in range(m):
        a, b = map(lambda x: int(x)-1, input().split())
        to[a].append(b)
        to[b].append(a)
        uf.unite(a, b)

    block = [[] for _ in range(n)]
    block_count = [0 for _ in range(n)]
    for _ in range(k):
        c, d = map(lambda x: int(x)-1, input().split())
        block[c].append(d)
        block[d].append(c)
        if uf.isSameGroup(c, d):
            block_count[c] += 1 
            block_count[d] += 1
   
    for i in range(n):
        ans = uf.getLength(i) - len(to[i]) - block_count[i] - 1
        print(ans, end=' ')
main()

Solution (Python):
class UnionFind:
  def __init__(self,n):
    self.n=n
    self.parent=[i for i in range(n)] 
    self.rank=[1]*n 
    self.size=[1]*n 
    
  def find(self,x): 
    if self.parent[x]==x:
      return x
    else:
      self.parent[x]=self.find(self.parent[x]) 
      return self.parent[x]
  
  def unite(self,x,y): 
    x=self.find(x)
    y=self.find(y)
    if x != y:
      if self.rank[x]<self.rank[y]:
        self.parent[x]=y
        self.size[y]+=self.size[x]
      else:
        self.parent[y]=x
        self.size[x]+=self.size[y]
        if self.rank[x]==self.rank[y]:
          self.rank[x]+=1
  
  def is_same(self,x,y): 
    return self.find(x)==self.find(y)
  
  def group_size(self,x): 
    return self.size[self.find(x)]
  
  def group_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.parent) if i==x]
  
  def group_count(self): 
    return len(self.roots())


N,M,K=map(int,input().split())
A,B=[0]*M,[0]*M
C,D=[0]*K,[0]*K

direct=[[] for i in range(N)]
uf=UnionFind(N)

for i in range(M):
  A[i],B[i]=map(int,input().split())
  A[i]-=1
  B[i]-=1
  direct[A[i]].append(B[i])
  direct[B[i]].append(A[i])
  uf.unite(A[i],B[i])

for i in range(K):
  C[i],D[i]=map(int,input().split())
  C[i]-=1
  D[i]-=1
  if uf.is_same(C[i],D[i]):
    direct[C[i]].append(D[i])
    direct[D[i]].append(C[i])

ans=[0]*N
for i in range(N):
  ans[i]=uf.group_size(i)-len(direct[i])-1
print(*ans)

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


fri = [0] * n
for a, b in friend:
    fri[a] += 1
    fri[b] += 1


class UnionFindTree:
    def __init__(self, n):
        self.parent = [i for i in range(n)]
        self.rank = [0] * 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 unite(self, x, y):
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            if self.rank[x] < self.rank[y]:
                self.parent[x] = y
            else:
                self.parent[y] = x
                if self.rank[x] == self.rank[y]:
                    self.rank[x] += 1

fri_fri = [0] * n
uft = UnionFindTree(n)
for a, b in friend:
    if uft.root(a) == uft.root(b):
        continue
    count = fri_fri[uft.root(a)] + fri_fri[uft.root(b)] + 1
    uft.unite(a, b)
    fri_fri[uft.root(a)] = count


for c, d in block:
    if uft.root(c) == uft.root(d):
        fri[c] += 1
        fri[d] += 1


ans = []
for i in range(n):
    ans.append(str(fri_fri[uft.root(i)] - fri[i]))

print(' '.join(ans))