Baekjoon Online Judge - 2606
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import collections
def bfs(start): queue = collections.deque() visited = [start] queue += graph[start] count = 0
while queue: node = queue.popleft() if node not in visited: queue += graph[node] visited.append(node) count += 1 return count
V = int(input()) E = int(input())
graph = [[] for _ in range(V+1)]
for _ in range(E): a, b = map(int, input().split()) graph[a].append(b) graph[b].append(a)
print(bfs(1))
|