문제 확인하기
2. 정답 코드 (Python, memory: 68780KB, time: 568ms)
import sys
sys.setrecursionlimit(10 ** 8)
def dfs(node):
global visit_count
visited[node] = True
answer[node] = visit_count
visit_count += 1
graph[node].sort(reverse=True)
for neighbor in graph[node]:
if not visited[neighbor]:
dfs(neighbor)
N, M, R = map(int, sys.stdin.readline().split())
graph = [[] for _ in range(N + 1)]
visited = [False] * (N + 1)
answer = [0] * (N + 1)
visit_count = 1
for _ in range(M):
x, y = map(int, sys.stdin.readline().split())
graph[x].append(y)
graph[y].append(x)
dfs(R)
for val in answer[1:]:
print(val)