Buggy python submission (verdict: Runtime Error):
import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]


    
    
def main():
    N, M = MI()
    mylist = [[] for i in range(N)]
    for i in range(M):
        a, b = MI()
        mylist[a-1].append(b-1)
        mylist[b-1].append(a-1)
    
    dic = defaultdict(int)
    
    visit = [True]*N
    index = 0

    for i in range(N):
        dq = deque()
        if visit[i]:
            dq.append(i)
            while dq:
                temp = dq.popleft()
                for j in mylist[temp]:
                    if visit[j]:
                        dic[index]+=1
                        visit[j] = False
                        dq.append(j)
            index+=1
                
        
        
        
    #グループ数
    all_group_num = sorted(dic.values())
    res = 0
    all_num = N
    min_num = 0
    group = len(all_group_num)
    if group < N:
        all_group_num = [1]*(N-group)+all_group_num
    
    
    
    while True:
        index = bisect_right(all_group_num, min_num)
        all_num -= (group-index)*(all_group_num[index]-min_num)
        res+= all_group_num[index]-min_num
        
        min_num = all_group_num[index]
        
        if all_num == 0:
            break
        
    print(res)

    
    
    
    
if __name__ == "__main__":
    main()

Fixed submission:
import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]


    
    
def main():
    N, M = MI()
    mylist = [[] for i in range(N)]
    for i in range(M):
        a, b = MI()
        mylist[a-1].append(b-1)
        mylist[b-1].append(a-1)
    
    dic = defaultdict(int)
    
    visit = [True]*N
    index = 0

    for i in range(N):
        dq = deque()
        if visit[i]:
            dq.append(i)
            while dq:
                temp = dq.popleft()
                for j in mylist[temp]:
                    if visit[j]:
                        dic[index]+=1
                        visit[j] = False
                        dq.append(j)
            index+=1
                
        
        
        
    #グループ数
    all_group_num = sorted(dic.values())
    res = 0
    all_num = N
    min_num = 0
    
    if sum(all_group_num) < N:
        all_group_num = [1]*(N-sum(all_group_num))+all_group_num
    group = len(all_group_num)

    while True:
        index = bisect_right(all_group_num, min_num)
        all_num -= (group-index)*(all_group_num[index]-min_num)
        res+= all_group_num[index]-min_num
        
        min_num = all_group_num[index]
        
        if all_num == 0:
            break
        
    print(res)

    
    
    
    
if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]

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

    
    
def main():
    N, M = MI()
    union = UnionFind(N)
    for i in range(M):
        a, b = MI()
        union.union(a-1, b-1)
        
    #グループ数
    all_group_member = union.all_group_members()
    all_group_num = sorted([len(i) for i in all_group_member.values()])
    res = 0
    all_num = N
    min_num = 0
    group = len(all_group_num)
    while True:
        index = bisect_right(all_group_num, min_num)
        all_num -= (group-index)*(all_group_num[index]-min_num)
        res+= all_group_num[index]-min_num
        
        min_num = all_group_num[index]
        
        if all_num == 0:
            break
        
    print(res)

    
    
    
if __name__ == "__main__":
    main()

Fixed submission:
import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]


    
    
def main():
    N, M = MI()
    mylist = [[] for i in range(N)]
    for i in range(M):
        a, b = MI()
        mylist[a-1].append(b-1)
        mylist[b-1].append(a-1)
    
    dic = defaultdict(int)
    
    visit = [True]*N
    index = 0

    for i in range(N):
        dq = deque()
        if visit[i]:
            dq.append(i)
            while dq:
                temp = dq.popleft()
                for j in mylist[temp]:
                    if visit[j]:
                        dic[index]+=1
                        visit[j] = False
                        dq.append(j)
            index+=1
                
        
        
        
    #グループ数
    all_group_num = sorted(dic.values())
    res = 0
    all_num = N
    min_num = 0
    
    if sum(all_group_num) < N:
        all_group_num = [1]*(N-sum(all_group_num))+all_group_num
    group = len(all_group_num)

    while True:
        index = bisect_right(all_group_num, min_num)
        all_num -= (group-index)*(all_group_num[index]-min_num)
        res+= all_group_num[index]-min_num
        
        min_num = all_group_num[index]
        
        if all_num == 0:
            break
        
    print(res)

    
    
    
    
if __name__ == "__main__":
    main()