Skip to content

Commit

Permalink
연결 요소의 개수 문제 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeVicTech committed Feb 11, 2023
1 parent 75fd2b7 commit c432014
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions 깊이or너비 우선 탐색/연결_요소의_개수.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# https://www.acmicpc.net/problem/11724

from collections import deque

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

graph = [[] for _ in range(n+1)]

for _ in range(m):
a,b = map(int,input().split())
graph[a].append(b)
graph[b].append(a)

visited = [0] * (n+1)

def bfs(start):
queue = deque()
queue.append(start)
visited[start] == 1

while queue:
value = queue.popleft()
for item in graph[value]:
if visited[item] == 0:
queue.append(item)
visited[item] = 1
cnt = 0

for i in range(1,n+1):
if visited[i] == 0:
bfs(i)
cnt += 1

print(cnt)






0 comments on commit c432014

Please sign in to comment.