0%

[백준/2606] 바이러스

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)
# visited 리스트에 노드를 넣을 때마다 count 1씩 증가
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))