문제
코드
from sys import setrecursionlimit, stdin
setrecursionlimit(10**6)
input = stdin.readline
def dfs(graph: list, visited: list, now: int):
global visit_order
visited[now] = visit_order
for x in graph[now]:
if not visited[x]:
visit_order += 1
dfs(graph, visited, x)
if __name__ == '__main__':
n, m, r = map(int, input().split())
graph = [[] for _ in range(n+1)]
visited = [0] * (n+1)
visit_order = 1
for _ in range(m):
u, v = map(int, input().split())
graph[u].append(v)
graph[v].append(u)
for x in range(n+1):
graph[x].sort()
dfs(graph, visited, r)
for x in range(1, n+1):
print(visited[x])
결과
출처 & 깃허브
boj 24479
github