문제 링크 - https://www.acmicpc.net/problem/1260
from collections import deque
n, m, v = map(int, input().split())
graph = [[] for _ in range(n+1)]
visited = [False] * (n+1)
for _ in range(m):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
for i in range(len(graph)):
graph[i].sort()
def dfs(start):
visited[start] = True
print(start, end=' ')
for i in graph[start]:
if not visited[i]:
visited[i] = True
dfs(i)
def bfs(start):
visited[start] = True
queue = deque()
queue.append(start)
while queue:
x = queue.popleft()
print(x, end = ' ')
for i in graph[x]:
if not visited[i]:
visited[i] = True
queue.append(i)
dfs(v)
print()
visited = [False] * (n+1)
bfs(v)