from collections import deque
#import sys
#input = sys.stdin.readline
n, m = map(int,input().split())
arr = [[] for _ in range(n + 1)]
visit = [False] * (n+1)
visit[0] = True
cnt = 0
for i in range(m):
a, b = map(int,input().split())
arr[a].append(b)
arr[b].append(a)
def bfs(v):
q = deque()
q.append(v)
visit[v] = True
while q:
v = q.popleft()
for i in arr[v]:
if visit[i] == False:
q.append(i)
visit[i] = True
while not all(visit):
v = visit.index(False)
bfs(v)
cnt += 1
print(cnt)
연결 요소가 뭔가 했는데 간선으로 연결된 그래프 덩어리 단위를 말하는 듯
어제 사용했던 BFS 코드를 살짝 수정해서 문제를 풀었다.
10분 컷
'배우는 것들 > python' 카테고리의 다른 글
[ERROR] volatile gpu utils 0, GPU 병목 (0) | 2024.03.04 |
---|---|
백준 16929- Two Dots (0) | 2022.08.05 |
백준 1260- DFS와 BFS (0) | 2022.07.25 |
백준 1463 - 1로 만들기 (0) | 2022.07.06 |
백준 11057번 - 오르막수 (0) | 2022.06.20 |