728x90
반응형
간단한 그래프 탐색 문제이다. 그냥 DFS 방식으로 풀었다.
def dfs(node):
for n in graph[node]:
if check[n] == 0:
check[n] = check[node]+1
dfs(n)
for _ in range(int(input())):
N = int(input())
graph = [[] for _ in range(N+1)]
for i in range(1, N+1):
v = int(input())
graph[i].append(v)
check = [0]*(N+1)
dfs(1)
print(check[N] if check[N] > 0 else 0)
728x90
반응형
'Agorithm > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 13458번 시험 감독(python) (0) | 2021.03.11 |
---|---|
백준 알고리즘 11559번 Puyo Puyo(python) (0) | 2021.03.11 |
백준 알고리즘 2644번 촌수계산(python) (0) | 2021.03.11 |
백준 알고리즘 11725번 트리의 부모 찾기(python) (0) | 2021.03.11 |
백준 알고리즘 10026번 적록색약(python) (0) | 2021.03.11 |