링크 : https://www.acmicpc.net/problem/1260
/*
문제 : DFS와 BFS
링크 : https://www.acmicpc.net/problem/1260
*/
#include <iostream>
#include <queue>
using namespace std;
int N, M, V;
int arr[1001][1001];
bool visited[1001];
int cnt = 0;
queue<int> q;
void bfs(int n){
q.push(n);
visited[n] = true;
cout << n << " ";
while(!q.empty()){
int f = q.front();
q.pop();
for(int i = 1; i <= N; i++){
if(arr[f][i] == 1 && !visited[i]) {
q.push(i);
visited[i] = true;
cout << i << " ";
}
}
}
}
void dfs(int n){
visited[n] = true;
cout << n << " ";
for(int i = 1; i <= N; i++){
if(arr[n][i] == 1 && !visited[i]) dfs(i);
}
}
int main(){
cin >> N >> M >> V;
for(int i = 0; i < M; i++){
int x, y;
cin >> y >> x;
arr[y][x] = 1;
arr[x][y] = 1;
}
dfs(V);
for(int i = 1; i <= N; i++){
visited[i] = false;
}
cout << '\n';
bfs(V);
return 0;
}