문제링크: https://school.programmers.co.kr/learn/courses/30/lessons/132266
from collections import deque
def solution(n, roads, sources, destination):
visited = [-1] * (n+1)
graph = [[] for _ in range(n+1)]
for a,b in roads:
graph[a].append(b)
graph[b].append(a)
q = deque()
q.append(destination)
visited[destination] = 0
while q:
now = q.popleft()
for node in graph[now]:
if visited[node] == -1:
visited[node] = visited[now] + 1
q.append(node)
return [visited[i] for i in sources]