from collections import deque
def dfs(g):
visited[g] = True
result_dfs.append(g)
for i in graph[g]:
if not visited[i]:
dfs(i)
def bfs(g):
q = deque([g])
visited[g] = True
result = [g]
while q:
t = q.popleft()
for i in graph[t]:
if not visited[i]:
visited[i] = True
q.append(i)
result.append(i)
return result
n, m, v = [int(v) for v in input().split()]
graph = [[] for _ in range(n+1)]
for _ in range(m):
a, b = [int(v) for v in input().split()]
graph[a].append(b)
graph[b].append(a)
for i in range(len(graph)):
graph[i].sort()
visited = [False] * (n+1)
result_dfs = []
dfs(v)
visited = [False] * (n+1)
result_bfs = bfs(v)
print(*result_dfs)
print(*result_bfs)
dfs와 bfs 개념을 확실하게 다져줄 수 있는 문제