

첫째줄 : 전체 사람의 수 n
둘째줄: 촌수를 계산해야하는 서로 다른 두사람의 번호
셋째줄 : 부모 자신들간의 관계의 개수 m
넷째줄 : 부모 자식간의 관계를 나타내는 두 번호 x,y(앞에 나오는 번호 x는 뒤에 나오는 정수 y의 부모 번호)
import sys
from collections import deque
# 입력을 위한 readline 사용
input = sys.stdin.readline
# 정수 입력
n = int(input().strip())
num1, num2 = map(int,input().split())
m = int(input().strip())
visited =[False] *(n+1)
arr =[[] for _ in range(n+1)]
for i in range(m):
a, b = map(int, input().split())
arr[a].append(b)
arr[b].append(a)
depth =-1
# BFS를 사용하여 두 노드 간의 촌수를 찾는 함수
def bfs(start, end):
global depth
queue = deque([(start, 0)]) # 노드와 깊이를 큐에 저장
visited[start] = True
while queue:
current, d = queue.popleft()
if current == end:
depth = d
return
for neighbor in arr[current]:
if not visited[neighbor]:
visited[neighbor] = True
queue.append((neighbor, d + 1))
# 시작 노드와 끝 노드로 BFS 수행
bfs(num1, num2)
print(depth)
def dfs(current,end,dep):
global depth
if current == end:
if depth==-1 or dep<depth:
depth =dep
return
visited[current] =True
for n in arr[current]:
if not visited[n]:
dfs(n,end,dep+1)
visited[current] = False
dfs(num1,num2,0)
print(depth)