DHistory
[Baekjoon] DFS/BFS - 2606 바이러스 본문
문제
2606번: 바이러스
첫째 줄에는 컴퓨터의 수가 주어진다. 컴퓨터의 수는 100 이하인 양의 정수이고 각 컴퓨터에는 1번 부터 차례대로 번호가 매겨진다. 둘째 줄에는 네트워크 상에서 직접 연결되어 있는 컴퓨터 쌍
www.acmicpc.net
풀이
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 |