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, M, K = map(int, input().split())

class UnionFind:
    def __init__(self, n):
        self.table = [-1 for _ in range(n)]
        self.size = [1]*n

    def _root(self, x):
        if self.table[x] < 0:
            return x
        else:
            self.table[x] = self._root(self.table[x])
            return self.table[x]

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

    def union(self, x, y):
        r1 = self._root(x)
        r2 = self._root(y)
        if r1 == r2:
            return
        d1 = self.table[r1]
        d2 = self.table[r2]
        if d1 <= d2:
            self.table[r2] = r1
            self.size[r1] += self.size[r2]
            if d1 == d2:
                self.table[r1] -= 1
        else:
            self.table[r1] = r2
            self.size[r2] += self.size[r1]

    def group_size(self, x):
        return self.size[self._root(x)]

Friends = [list(map(int, input().split())) for _ in range(M)]
Blocks = [list(map(int, input().split())) for _ in range(K)]

Friendship = UnionFind(N)
Cant = [0]*N

for a in Friends:
    Friendship.union(a[0]-1, a[1]-1)

for b in Friends:
    if Friendship.find(b[0]-1, b[1]-1):
        Cant[b[0]-1] += 1; Cant[b[1]-1] += 1

for c in Blocks:
    if Friendship.find(c[0]-1, c[1]-1):
        Cant[c[0]-1] += 1; Cant[c[1]-1] += 1

print(*[Friendship.group_size(i)-Cant[i]-1 for i in range(N)])

Solution (Python):
import sys
from collections import Counter as cc
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())


class UnionFind():
    def __init__(self, n):
        self.parent = [i for i in range(n)]
        self.height = [0]*n

    def get_root(self, i):
        if self.parent[i] == i:
            return i
        else:
            self.parent[i] = self.get_root(self.parent[i])
            return self.parent[i]
        
    def unite(self, i, j):
        root_i = self.get_root(i)
        root_j = self.get_root(j)
        if root_i != root_j:
            if self.height[root_i] < self.height[root_j]:
                self.parent[root_i] = root_j
            else:
                self.parent[root_j] = root_i
                if self.height[root_i] == self.height[root_j]:
                    self.height[root_i] += 1
    
    def is_in_group(self, i, j):
        if self.get_root(i) == self.get_root(j):
            return True
        else:
            return False


def main():
    n, m, k= mi()   
    e = [0]*n
    uf = UnionFind(n)     
    for i in range(m):
        a, b = mi()
        a-=1
        b-=1
        e[a]-= 1
        e[b]-= 1
        uf.unite(a,b)
    
    
    for _ in range(k):
        a,b = mi()
        a-=1
        b-=1
        if uf.is_in_group(a,b):
            e[a] -= 1
            e[b] -= 1
        
    for i in range(n):
        uf.get_root(i)
    s = cc(uf.parent)
    for i in range(n):
        x = uf.get_root(i)
        e[i] += s[x]-1
    print(*e)




if __name__ == '__main__':
    main()

Solution (Python):

class Uf:
    def __init__(self, N):
        self.p = list(range(N))
        self.rank = [0] * N
        self.size = [1] * N

    def root(self, x):
        if self.p[x] != x:
            self.p[x] = self.root(self.p[x])

        return self.p[x]

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

    def unite(self, x, y):
        u = self.root(x)
        v = self.root(y)

        if u == v: return

        if self.rank[u] < self.rank[v]:
            self.p[u] = v
            self.size[v] += self.size[u]
            self.size[u] = 0
        else:
            self.p[v] = u
            self.size[u] += self.size[v]
            self.size[v] = 0

            if self.rank[u] == self.rank[v]:
                self.rank[u] += 1

    def count(self, x):
        return self.size[self.root(x)]

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

uf = Uf(N+1)
M = [-1] * (N+1)
for a, b in AB:
    uf.unite(a, b)
    M[a] -= 1
    M[b] -= 1
for c, d in CD:
    if uf.same(c, d):
        M[c] -= 1
        M[d] -= 1
for i in range(1, N+1):
    M[i] += uf.count(i)
print(" ".join(map(str, M[1:])))

Solution (Python):
import sys
N, M, K = map(int, input().split())
friends = [set() for _ in range(N)]
for _ in range(M):
    a, b = map(int, sys.stdin.readline().split())
    friends[a-1].add(b-1)
    friends[b-1].add(a-1)
blocks = [set() for _ in range(N)]
for _ in range(K):
    c, d = map(int, sys.stdin.readline().split())
    blocks[c-1].add(d-1)
    blocks[d-1].add(c-1)

done = set()
chains = []
todo = []
for s in range(N):
    if s in done:
        continue
    chain = set()
    todo.append(s)
    while todo:
        i = todo.pop()
        for ni in friends[i]:
            if ni in done:
                continue
            done.add(ni)
            chain.add(ni)
            todo.append(ni)
    chains.append(chain)

ans = [0] * N
for chain in chains:
    for i in chain:
        blocks[i].intersection_update(chain)
        ans[i] = len(chain) - len(blocks[i]) - len(friends[i]) - 1
print(" ".join(map(str, ans)))

Solution (Python):
class UnionFind:
    def __init__(self, n: int) -> None:
        self.forest = [-1] * n

    def union(self, x: int, y: int) -> None:
        x = self.findRoot(x)
        y = self.findRoot(y)
        if x == y:
            return
        if self.forest[x] > self.forest[y]:
            x,y = y,x
        self.forest[x] += self.forest[y]
        self.forest[y] = x
        return

    def findRoot(self, x: int) -> int:
        if self.forest[x] < 0:
            return x
        else:
            self.forest[x] = self.findRoot(self.forest[x])
            return self.forest[x]

    def issame(self, x: int, y: int) -> bool:
        return self.findRoot(x) == self.findRoot(y)
    
    def size(self,x: int) -> int:
        return -self.forest[self.findRoot(x)]

n,m,k = map(int,input().split())
l = [[] for _ in range(n)]
u = UnionFind(n)
for _ in range(m):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    u.union(a,b)
    l[a].append(b)
    l[b].append(a)

for _ in range(k):
    c,d = map(int,input().split())
    c -= 1
    d -= 1
    if u.issame(c,d):
        l[c].append(d)
        l[d].append(c)

for i in range(n):
    q = u.size(i)
    w = len(l[i])
    print(q-w-1, end=" ")

Solution (Python):

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


def resolve():
    N, M, K = map(int, input().split())
    uf = UnionFind(N)
    sub_friend = [1] * N  

    frind = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(lambda x: int(x) - 1, input().split())
        frind[a].append(b)
        frind[b].append(a)
        sub_friend[a] += 1
        sub_friend[b] += 1

    block = [[] 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)

    for i in range(N):
        for to in frind[i]:
            uf.union(i, to)

    ans = [uf.size(i) - sub_friend[i] for i in range(N)]

    for i in range(N):
        for bl in block[i]:
            if uf.is_same(i, bl):
                ans[i] -= 1
    print(*ans)


if __name__ == "__main__":
    resolve()

Solution (Python):

import sys
sys.setrecursionlimit(10 ** 7)

def resolve():
    N, M,K = map(int, input().split())
    frind = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(lambda x:int(x)-1, input().split())
        frind[a].append(b)
        frind[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)


    D = {}
    parent = [-1]*N
    visited = [False]*N
    for root in range(N):
        if visited[root]:
            continue
        D[root] = set([root])
        stack = [root]
        while stack:
            n = stack.pop()
            visited[n] = True
            parent[n] = root
            for to in frind[n]:
                if visited[to]:
                    continue
                D[root].add(to)
                stack.append(to)

    ans = [0]*N
    for i in range(N):
        gr = D[parent[i]]
        tmp = len(gr)-len(frind[i]) - 1
        for bl in block[i]:
            if bl in gr:
                tmp -= 1
        ans[i] = tmp
    print(*ans)


if __name__ == "__main__":
    resolve()

Solution (Python):
from collections import deque, Counter
import sys

N_MAX = 100000 + 5
sys.setrecursionlimit(N_MAX)

N, M, K = map(int, sys.stdin.readline().rstrip().split())

fr = [[] for _ in range(N)]  
for _ in range(M):
    a, b = map(int, sys.stdin.readline().rstrip().split())
    fr[a - 1].append(b - 1)
    fr[b - 1].append(a - 1)

bl = [[] for _ in range(N)]  
for _ in range(K):
    a, b = map(int, sys.stdin.readline().rstrip().split())
    bl[a - 1].append(b - 1)
    bl[b - 1].append(a - 1)


gr = [0] * N  



def bfs(u, num):

    q = deque()
    q.append(u)
    gr[u] = num
    while q:
        u = q.popleft()
        for v in fr[u]:
            if gr[v] == 0:  
                gr[v] = num  
                q.append(v)


num = 1
for i in range(N):
    if gr[i] == 0:
        bfs(i, num)
        num += 1



gr_num = Counter(gr)  



ans = []
for i in range(N):
    kouho_suu = gr_num[gr[i]]
    for f in fr[i]:
        if gr[i] == gr[f]:
            kouho_suu -= 1
    for b in bl[i]:
        if gr[i] == gr[b]:
            kouho_suu -= 1
    kouho_suu -= 1

    ans.append(kouho_suu)

print(*ans)

Solution (Python):


class UnionFind():
  def __init__(self, n):
    self.n = n                           
    self.parents = [i for i in range(n)] 
    self.rank = [0] * n                  
    self.size = [1] * 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 union(self, x, y):
    root_x = self.find(x)
    root_y = self.find(y)
    
    if root_x != root_y:
      
      if self.rank[root_x] > self.rank[root_y]:
        self.parents[root_y] = root_x
        self.size[root_x] += self.size[root_y]
      else:
        self.parents[root_x] = root_y
        self.size[root_y] += self.size[root_x]
        if self.rank[root_x] == self.rank[root_y]:
          self.rank[root_y] += 1

N, M, K = map(int, input().split())
A = list()
B = list()
C = list()
D = list()
union_find = UnionFind(n=N)
direct_and_block_count = [0] * N
ans = ''
for i in range(M):
  a, b = map(int, input().split())
  A.append(a-1)
  B.append(b-1)
  union_find.union(a-1, b-1)
  direct_and_block_count[a-1] += 1
  direct_and_block_count[b-1] += 1
for i in range(K):
  c, d = map(int, input().split())
  C.append(c-1)
  D.append(d-1)
  if union_find.find(c-1) == union_find.find(d-1):
    
    direct_and_block_count[c-1] += 1
    direct_and_block_count[d-1] += 1

ans = [0] * N
for i in range(N):
  
  ans[i] = union_find.size[union_find.find(i)] - direct_and_block_count[i] - 1
print(*ans)

Solution (Python):
import sys
import itertools


class UnionFindTree:
    def __init__(self, n: int) -> None:
        self.par = list(range(n))
        self.rank = [0] * n

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

    def unite(self, x: int, y: int) -> None:
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        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(self, x: int, y: int) -> bool:
        return self.find(x) == self.find(y)


def resolve(in_):
    N, M, K = map(int, in_.readline().split())
    ab = tuple(tuple(map(int, line.split())) for line in itertools.islice(in_, M))
    cd = tuple(tuple(map(int, line.split())) for line in itertools.islice(in_, K))

    ng = [set() for _ in range(N + 1)]
    friends_tree = UnionFindTree(N + 1)

    for a, b in ab:
        friends_tree.unite(a, b)
    for a, b in ab:
        if friends_tree.same(a, b):
            ng[a].add(b)
            ng[b].add(a)
    for c, d in cd:
        if friends_tree.same(c, d):
            ng[c].add(d)
            ng[d].add(c)
    ok = [0] * (N + 1)
    for i in range(1, N + 1):
        ok[friends_tree.find(i)] += 1    

    ans = [0] * (N + 1)
    for i in range(1, N + 1):
        ans[i] = ok[friends_tree.find(i)] - len(ng[i]) - 1

    return ' '.join(map(str, ans[1:]))


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

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

par = [-1]*n
num = [0]*n

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

def union(x, y):
    p,q=find(x), find(y)
    if p==q:
        return
    if p>q:
        p,q = q,p
    par[p] += par[q]
    par[q] = p

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

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

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

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

for i in range(n):
    print(size(i)-1-num[i], end=" ")

Solution (Python):
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())
fr = [0]*n
uf_fr = UnionFind(n)

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

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

ans_list = []
for i in range(n):
    ans_list.append(uf_fr.size(i) - fr[i] - 1)
print(*ans_list)

Solution (Python):
N, M, K = list(map(int, input().split()))
p = [0] * N 
for i in range(N):
    p[i] = [0]*3
    p[i][0] = []
    p[i][1] = []
    p[i][2] = -1
g = [] 
stack = []
groupNum = 0
fcs = []

for i in range(M):
    a, b = list(map(int, input().split()))
    p[a-1][0].append(b-1)
    p[b-1][0].append(a-1)

def groupmake(person,group):
    groupSize = 0
    stack.append(person)
    p[person][2] = group
    while True:
        num = stack.pop()
        groupSize += 1
        for i in p[num][0]:
            if p[i][2] == -1:
                stack.append(i)
                p[i][2] = group
        if len(stack) == 0:
            return groupSize

for i in range(len(p)):
    if p[i][2] == -1:
        g.append(groupmake(i, groupNum))
        groupNum += 1

for person in p:
    fcs.append(g[person[2]] - len(person[0]) -1)

for i in range(K):
    c, d = list(map(int, input().split()))
    if p[c-1][2] == p[d-1][2]:
        fcs[c-1] -= 1
        fcs[d-1] -= 1

print(" ".join(map(str, fcs)))

Solution (Python):
import collections

n, m, k = map(int, input().split())
ab = [[] for i in range(n + 1)]
cd = [[] for i in range(n + 1)]
for i in range(m):
    a, b = map(int, input().split())
    ab[a].append(b)
    ab[b].append(a)
for i in range(k):
    c, d = map(int, input().split())
    cd[c].append(d)
    cd[d].append(c)
flag = [False for i in range(n + 1)]
groups = []
num = 1
while num <= n:
    if flag[num] == True:
        num += 1
        continue
    else:
        temp = set()
        queue = collections.deque([num])
        while queue:
            temp2 = queue.popleft()
            temp |= {temp2}
            flag[temp2] = True
            for i in ab[temp2]:
                if i not in temp:
                    queue.append(i)
        groups.append(temp)
group = [None for i in range(n + 1)]
for i in range(len(groups)):
    for j in groups[i]:
        group[j] = i
for i in range(1, n + 1):
    temp = groups[group[i]]
    temp2 = temp & set(cd[i])
    temp3 = temp & set(ab[i])
    print(len(temp) - 1 - len(temp2) - len(temp3), end = " ")

Solution (Python):
class UnionFind:
    def __init__(self, n: int) -> None:
        self.forest = [-1] * n

    def union(self, x: int, y: int) -> None:
        x = self.findRoot(x)
        y = self.findRoot(y)
        if x == y:
            return
        if self.forest[x] > self.forest[y]:
            x, y = y, x
        self.forest[x] += self.forest[y]
        self.forest[y] = x
        return

    def findRoot(self, x: int) -> int:
        if self.forest[x] < 0:
            return x
        else:
            self.forest[x] = self.findRoot(self.forest[x])
            return self.forest[x]

    def issame(self, x: int, y: int) -> bool:
        return self.findRoot(x) == self.findRoot(y)

    def size(self, x: int) -> int:
        return -self.forest[self.findRoot(x)]


n, m, k = map(int, input().split())
u = UnionFind(n)
dire = [[] for i in range(n)]
for i in range(m):
    a, b = map(int, input().split())
    dire[a - 1].append(b - 1)
    dire[b - 1].append(a - 1)
    u.union(a - 1, b - 1)
for i in range(k):
    c, d = map(int, input().split())
    if u.issame(c-1,d-1):
        dire[c - 1].append(d - 1)
        dire[d - 1].append(c - 1)

print(*[u.size(i) - len(dire[i]) - 1 for i in range(n)])

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

friends = [set() for _ in range(N)]
for _ in range(M):
    a,b = map(lambda x: int(x)-1, input().split())
    friends[a].add(b)
    friends[b].add(a)

blocks = [set() for _ in range(N)]
for _ in range(K):
    c,d = map(lambda x: int(x)-1, input().split())
    blocks[c].add(d)
    blocks[d].add(c)

ans = [0]*N
visited = [False]*N
for i in range(N):
    if visited[i]:
        continue
    visited[i] = True
    d = [i]
    check = {i} 
    while len(d):
        tmp = d.pop()
        for k in friends[tmp]:
            if visited[k]:
                continue
            d.append(k)
            visited[k] = True
            check.add(k)
    for j in check:
        ans[j] += len(check) - len(friends[j]&check) - len(blocks[j]&check) - 1
print(*ans)

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline

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

ab = [list(map(int, input().split())) for _ in range(m)]
cd = [list(map(int, input().split())) for _ in range(k)]

ff = [[] for _ in range(n+1)]
for a, b in ab:
    ff[a].append(b)
    ff[b].append(a)

visited = [False] * (n+1)
visited[0] = True

com = [-1] * (n+1)


def dfs(v, ff, visited, com, g):
    q = deque([v])
    visited[v] = True
    com[v] = g
    k = 1
    while len(q) > 0:
        w = q.pop()
        for x in ff[w]:
            if not visited[x]:
                q.append(x)
                visited[x] = True
                com[x] = g
                k += 1
    return k

g = 0
group_num = []

for i in range(1, n+1):
    if visited[i]:
        pass
    else:
        k = dfs(i, ff, visited, com, g)
        group_num.append(k)
        g += 1



friends = [0] * (n+1)

for i in range(1, n+1):
    friends[i] += group_num[com[i]] - 1

for a, b in ab:
    if com[a] == com[b]:
        friends[a] -= 1
        friends[b] -= 1

for c, d in cd:
    if com[c] == com[d]:
        friends[c] -= 1
        friends[d] -= 1

print(" ".join(map(str, friends[1:])))

Solution (Python):
class UnionFind:
    def __init__(self, numV):
        self.pars = list(range(numV))
        self.ranks = [0] * numV
        self.sizes = [1] * numV
    def getRoot(self, x):
        par = self.pars[x]
        if par != x:
            self.pars[x] = par = self.getRoot(par)
        return par
    def merge(self, x, y):
        x, y = self.getRoot(x), self.getRoot(y)
        sx, sy = self.sizes[x], self.sizes[y]
        if x == y: return (0, 0)
        if self.ranks[x] < self.ranks[y]:
            self.pars[x] = y
            self.sizes[y] += sx
        else:
            self.pars[y] = x
            self.sizes[x] += sy
            if self.ranks[x] == self.ranks[y]:
                self.ranks[x] += 1
        return (sx, sy)
    def isSame(self, x, y):
        return self.getRoot(x) == self.getRoot(y)
    def updatePars(self):
        for v in range(len(self.pars)):
            self.getRoot(v)
    def getSize(self, x):
        return self.sizes[self.getRoot(x)]

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

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

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


ans = []
for i in range(n):
    r = uf.getRoot(i)
    ans.append(uf.sizes[r] - len(list(set(ex[i]))) - 1)
ans = [str(i) for i in ans]
print(' '.join(ans))

Solution (Python):
from collections import defaultdict


class DisjoinSet:
    def __init__(self, N):
        self.par = [-1] * N
        self.sz = [1] * N

    def root(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.root(self.par[x])
            return self.par[x]

    def unite(self, a, b):
        a = self.root(a)
        b = self.root(b)
        if a == b:
            return
        if self.sz[a] > self.sz[b]:
            a, b = b, a
        self.par[a] = b
        self.sz[b] += self.sz[a]

    def same(self, a, b):
        return self.root(a) == self.root(b)

    def size(self, x):
        return self.sz[self.root(x)]

    def __str__(self):
        clusters = defaultdict(list)
        for x in range(N):
            clusters[self.root(x)].append(x)
        return str(list(clusters.values()))


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

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

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

data = DisjoinSet(N)
for a, b in zip(A, B):
    data.unite(a, b)

ans = [data.size(x) - 1 for x in range(N)]

for a, b in zip(A, B):
    ans[a] -= 1
    ans[b] -= 1

for c, d in zip(C, D):
    if data.same(c, d):
        ans[c] -= 1
        ans[d] -= 1

print(' '.join(map(str, ans)))

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

class UnionFindPathCompression():
    def __init__(self, n):
        self.parents = list(range(n))
        self.rank = [1]*n
        self.group_size = [1]*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 union(self, x, y):
        
        px = self.find(x)
        py = self.find(y)

        if px == py:
            return
        else:
            if self.rank[px] < self.rank[py]:
                self.parents[px] = py
                self.group_size[py] += self.group_size[px]
            else:
                self.parents[py] = px
                self.group_size[px] += self.group_size[py]
                
                if self.rank[px] == self.rank[py]:
                    self.rank[px] += 1
                
n,m,k = map(int,input().split())
ufpc = UnionFindPathCompression(n)
friends = [[] for i in range(n)]
ans = [-1]*n
for i in range(m):
    a, b = (int(x) for x in input().split())
    a, b = a-1, b-1
    friends[a].append(b)
    friends[b].append(a)
    ufpc.union(a,b)

for i in range(k):
    c, d = (int(x) for x in input().split())
    c, d = c-1, d-1
    if ufpc.find(c) == ufpc.find(d):
        ans[c] -= 1
        ans[d] -= 1

for i in range(n):
    ans[i] += ufpc.group_size[ufpc.find(i)]
    ans[i] -= len(friends[i])

print(*ans)

Solution (Python):




import sys
import os
sys.setrecursionlimit(1000000)
from collections import defaultdict
adj = defaultdict(list)
adj1=defaultdict(list)
szc = [ 0 for i in range(300000)]
which =[0 for i in range(300000)]
vis = [0 for i in range(300000)]
tot =0
def dfs1(u):
	global tot
	vis[u]=1
	which[u]=tot
	szc[tot]=szc[tot]+1
	for i in adj[u]:
		if vis[i]==1:
			continue
		dfs1(i)
def main():
	global tot
	n , m , k = map(int, input().split())
	bk =[0 for i in range(n+1)]
	fd =[0 for i in range(n+1)]
	for i in range(m):
		x, y = map(int ,input().split())
		adj[x].append(y)
		adj[y].append(x)
		fd[x]=fd[x]+1
		fd[y]=fd[y]+1
	for i in range(k):
		x, y = map(int , input().split())
		adj1[x].append(y)
		adj1[y].append(x)
	for i in range(1, n+1):
		if vis[i]!=1:
			tot = tot+1
			dfs1(i)
	ans=[]
	for i in range(1, n+1):
		res = szc[which[i]]
		res = res-fd[i]
		res = res-1
		for j in adj1[i]:
			if which[j]==which[i]:
				res =res -1
		ans.append(res)
	print(*ans)
		
if __name__=="__main__":
	main()

Solution (Python):
import sys
import os
sys.setrecursionlimit(1000000)
from collections import defaultdict
adj = defaultdict(list)
adj1=defaultdict(list)
szc = [ 0 for i in range(300000)]
which =[0 for i in range(300000)]
vis = [0 for i in range(300000)]
tot =0
def dfs1(u):
	global tot
	vis[u]=1
	which[u]=tot
	szc[tot]=szc[tot]+1
	for i in adj[u]:
		if vis[i]==1:
			continue
		dfs1(i)
def main():
	global tot
	n , m , k = map(int, input().split())
	bk =[0 for i in range(n+1)]
	fd =[0 for i in range(n+1)]
	for i in range(m):
		x, y = map(int ,input().split())
		adj[x].append(y)
		adj[y].append(x)
		fd[x]=fd[x]+1
		fd[y]=fd[y]+1
	for i in range(k):
		x, y = map(int , input().split())
		adj1[x].append(y)
		adj1[y].append(x)
	for i in range(1, n+1):
		if vis[i]!=1:
			tot = tot+1
			dfs1(i)
	ans=[]
	for i in range(1, n+1):
		res = szc[which[i]]
		res = res-fd[i]
		res = res-1
		for j in adj1[i]:
			if which[j]==which[i]:
				res =res -1
		ans.append(res)
	print(*ans)
		
if __name__=="__main__":
	main()

Solution (Python):

class UnionFind:
    def __init__(self, n):
        self.par = list(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] = self.find(self.par[x])
            return self.par[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.rank[x] += 1
        elif self.rank[x] < self.rank[y]:
            x, y = y, x

        self.par[y] = x
        self.size[x] += self.size[y]

    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 = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(M)]
Y = [list(map(int, input().split())) for _ in range(K)]

friend = [[] for _ in range(N + 1)]
for a, b in X:
    friend[a].append(b)
    friend[b].append(a)

blocked = [[] for _ in range(N + 1)]
for c, d in X:
    blocked[c].append(d)
    blocked[d].append(c)

tree = UnionFind(N + 1)
for a, b in X:
    tree.unite(a, b)

ans = [0] * (N + 1)
for i in range(1, N + 1):
    ans[i] = tree.get_size(i) - len(friend[i]) - 1

for c, d in Y:
    if tree.is_same(c, d):
        ans[c] -= 1
        ans[d] -= 1

print(*ans[1:])