We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 75fd2b7 commit c432014Copy full SHA for c432014
깊이or너비 우선 탐색/연결_요소의_개수.py
@@ -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