n=int(input()) # 컴퓨터 개수 = 노드 개수
m=int(input()) # 연결된 컴퓨터 쌍 수 = 간선 개수
# 인접행렬
graph=[[0]*(n+1) for _ in range(n+1)]
for _ in range(m):
x,y=list(map(int,input().split()))
graph[x][y]=graph[y][x]=1
visited=[0]*(n+1) # 방문 체크에서 연결되었다는 표시인 1의 개수를 세면된다
def dfs(v):
visited[v]=1
for i in range(n+1):
if graph[i][v]==1 and visited[i]==0:
dfs(i)
dfs(1) # 1번 노드에 연결된 노드의 개수만 구하면 된다.
print(sum(visited)-1) # 1번 컴퓨터는 빼준다.
'알고리즘' 카테고리의 다른 글
[백준 알고리즘] 1197번 최소 스패닝 트리. 파이썬(python) + lambda, union-find (0) | 2022.05.04 |
---|---|
[백준 알고리즘] 1388번 바닥 장식. 파이썬(python) (0) | 2022.04.30 |
[백준 알고리즘] 2667번 단지번호붙이기. 파이썬(python) (0) | 2022.04.28 |
[백준 알고리즘] 1260번 DFS와 BFS. 파이썬(python) (0) | 2022.04.28 |
[백준 알고리즘] 1012번 유기농 배추. 파이썬(python) (0) | 2022.04.27 |