DHistory
[Baekjoon] DFS/BFS - 2606 바이러스 본문
문제
풀이
import sys
from collections import deque
n = int(sys.stdin.readline().rstrip())
count = int(sys.stdin.readline().rstrip())
edges = []
visited = [False for _ in range(n + 1)]
for _ in range(count):
a, b = map(int, sys.stdin.readline().rstrip().split())
edges.append((a, b))
def solution(n, edges, visited):
graphs = [[] for _ in range(n + 1)]
for a, b in edges:
graphs[a].append(b)
graphs[b].append(a)
queue = deque([1])
visited[1] = True
while queue:
now = queue.popleft()
for v in graphs[now]:
if not visited[v]:
visited[v] = True
queue.append(v)
return visited.count(True) - 1
print(solution(n, edges, visited))
채점 결과
'Computer Science > Algorithm' 카테고리의 다른 글
[Baekjoon] DFS/BFS - 1012 유기농 배추 (0) | 2023.09.04 |
---|---|
[Baekjoon] DFS/BFS - 1260 DFS와 BFS (0) | 2023.09.04 |
[Baekjoon] DFS/BFS - 16173 점프왕 쩰리 (Small) (0) | 2023.09.04 |
[Baekjoon] Greedy - 17503 맥주 축제 (0) | 2023.08.30 |
[Baekjoon] Greedy - 16206 롤케이크 (0) | 2023.08.30 |