촌수계산

안정은·2023년 9월 12일

코딩테스트

목록 보기
10/10

풀었어용~! 낄낄낄

n = int(input())+1
a,b = map(int,input().split())

G = [[] for _ in range(n)]

for _ in range(int(input())):
    i,j = map(int,input().split())
    G[i].append(j)
    G[j].append(i)

m = [0]*n

def bfs(G,v,m):
    Q = [v]
    m[v] = 1
    while Q:
        p = Q.pop(0)
        for i in G[p]:
            if not m[i]:
                Q.append(i)
                m[i] = m[p]+1

bfs(G,a,m)
print(m[b]-1)

m[i] = m[p]+1 크으~ 이 코드 생각해내기 정말 어려웠다! 자기 전 생각나서 까먹지 않고 오늘 해결~! 이게 다 나동빈씨 덕분입니다

profile
ㅎㅇ

0개의 댓글