https://www.acmicpc.net/problem/1260
from collections import deque
def dfs(graph, v, visited):
visited[v] = True
print(v, end = " ")
for i in graph[v]:
if not visited[i]:
dfs(graph, i, visited)
def bfs(graph, s, visited):
que = deque([s])
visited[s] = True
while que:
v = que.popleft()
print(v, end = " ")
for i in graph[v]:
if not visited[i]:
que.append(i)
visited[i] = True
n,m,v = map(int, input().split())
graph = [[] for _ in range(n+1)]
for _ in range(m):
temp_li = list(map(int, input().split()))
graph[temp_li[0]].append(temp_li[1])
graph[temp_li[1]].append(temp_li[0])
for i in range(n+1):
graph[i].sort()
visited = [False]*(n+1)
dfs(graph,v,visited)
print()
visited = [False]*(n+1)
bfs(graph,v,visited)