문제
코드
from sys import setrecursionlimit, stdin
setrecursionlimit(10**6)
input = stdin.readline
def dfs(graph: list, visited: list, start: int):
global visited_order
visited[start] = visited_order
for x in graph[start]:
if not visited[x]:
visited_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)
visited_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(reverse=True)
dfs(graph, visited, r)
for x in visited[1:]:
print(x)
결과
출처 & 깃허브
boj 24480
github