Skip to content

Commit c432014

Browse files
committed
연결 요소의 개수 문제 추가
1 parent 75fd2b7 commit c432014

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# https://www.acmicpc.net/problem/11724
2+
3+
from collections import deque
4+
5+
n,m = map(int,input().split())
6+
7+
graph = [[] for _ in range(n+1)]
8+
9+
for _ in range(m):
10+
a,b = map(int,input().split())
11+
graph[a].append(b)
12+
graph[b].append(a)
13+
14+
visited = [0] * (n+1)
15+
16+
def bfs(start):
17+
queue = deque()
18+
queue.append(start)
19+
visited[start] == 1
20+
21+
while queue:
22+
value = queue.popleft()
23+
for item in graph[value]:
24+
if visited[item] == 0:
25+
queue.append(item)
26+
visited[item] = 1
27+
cnt = 0
28+
29+
for i in range(1,n+1):
30+
if visited[i] == 0:
31+
bfs(i)
32+
cnt += 1
33+
34+
print(cnt)
35+
36+
37+
38+
39+
40+

0 commit comments

Comments
 (0)