https://www.acmicpc.net/problem/1260
from collections import deque
def dfs(w):
check[w]=True
print(w,end=' ')
for i in range(1,n+1):
if edge[w][i]==1 and check[i]==False: dfs(i)
def bfs(s):
q=deque()
q.append(s)
check1[s]=True
while len(q)>0:
s=q.popleft()
print(s,end=' ')
for i in range(1,n+1):
if edge[s][i]==1 and check1[i]==False:
q.append(i)
check1[i]=True
n,m,v=map(int,input().split())
edge=[[0 for _ in range(n+1)] for _ in range(n+1)]
check=[False for _ in range(n+1)]
check1=[False for _ in range(n+1)]
for i in range(m):
f,t=map(int,input().split())
edge[f][t]=edge[t][f]=1
dfs(v)
print()
bfs(v)