

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | from collections import deque def bfs(a, b): queue = deque([a]) visited[a] += 1 while queue: temp = queue.pop() for i in arr[temp]: if visited[i] != –1: continue queue.appendleft(i) visited[i] = visited[temp] + 1 if i == b: print(visited[i]) return print(–1) return n = int(input()) arr = [[] for _ in range(n + 1)] visited = [–1] * (n + 1) a, b = map(int, input().split()) t = int(input()) for _ in range(t): n1, n2 = map(int, input().split()) arr[n1].append(n2) arr[n2].append(n1) bfs(a, b) | cs |