백준 2606번: 바이러스 #Python

ColorlessDia·2025년 2월 19일

algorithm/baekjoon

목록 보기
458/807
import sys
from collections import deque

def BFS(graph, start):
    queue = deque([start])
    visited = [start]

    while 0 < len(queue):
        node = queue.popleft()

        for neighbor in graph[node]:

            if neighbor not in visited:
                queue.append(neighbor)
                visited.append(neighbor)

    return visited

input = sys.stdin.readline

computer = int(input())
network = int(input())

graph = dict()

for _ in range(network):
    a, b = map(int, input().split())

    if a not in graph:
        graph[a] = [b]
    else:
        graph[a] += [b]

    if b not in graph:
        graph[b] = [a]
    else:
        graph[b] += [a]

if 1 not in graph:
    print(0)
else:
    print(len(BFS(graph, 1)) - 1)

0개의 댓글