👩🏻🏫 풀이
def dfs(n):
visited[n] = True
print(n, end=' ')
for i in graph[n]:
if not visited[i]:
dfs(i)
from collections import deque
def bfs(n):
queue = deque([n])
visited[n] = True
while queue:
v = queue.popleft()
print(v, end=' ')
for i in graph[v]:
if not visited[i]:
queue.append(i)
visited[i] = True
n, m, v = map(int,input().split())
graph = [[] for _ in range(n+1)]
for i in range(m):
a,b = map(int,input().split())
graph[a].append(b)
graph[b].append(a)
for i in range(1,n+1):
graph[i].sort()
visited = [False] * (n+1)
dfs(v)
print( )
visited = [False] * (n+1)
bfs(v)