Algorithm/boj

[파이썬] 24482 알고리즘 수업 - 깊이 우선 탐색 4

takeU 2022. 8. 5. 15:23
반응형
import sys
sys.setrecursionlimit(10 ** 8)
input = sys.stdin.readline

n, m, r = map(int, input().split())
graph = [[] for _ in range(n + 1)]
visited = [-1] * (n + 1)

for _ in range(m):
    x, y = map(int, input().split())
    graph[x].append(y)
    graph[y].append(x)

def dfs(node):
    graph[node].sort(reverse=True)
    for g in graph[node]:
        if visited[g] == -1:
            visited[g] = visited[node] + 1
            dfs(g)

visited[r] = 0
dfs(r)

for v in visited[1:]:
    print(v)

dfs

반응형