백준 2606번 바이러스

Solution

import sys
from collections import deque

input = sys.stdin.readline

n = int(input())
m = int(input())
graph = [[] for _ in range(n + 1)]
visit = [0] * (n + 1)

for _ in range(m):
    x, y = list(map(int, input().split()))
    graph[x].append(y)
    graph[y].append(x)


def bfs(x):
    queue = deque([x])
    visit[x] = True
    while queue:
        v = queue.popleft()
        for i in graph[v]:
            if not visit[i]:
                queue.append(i)
                visit[i] = True


bfs(1)
print(sum(visit) - 1)