실버3 문제
from collections import deque
n = int(input())
m = int(input())
arr = [[0] * (n+1) for _ in range(n+1)]
visit_list = [0] * (n+1)
for _ in range(m):
a, b = map(int, input().split())
arr[a][b] = arr[b][a] = 1
birus = deque([1])
visit_list[1] = 1
cnt = -1
# bfs
while birus:
warm = birus.popleft()
cnt += 1
for i in range(1, n+1):
if ( arr[warm][i] == 1 and visit_list[i] == 0 ):
birus.append(i)
visit_list[i] = 1
print(cnt)