참고사항
dfs : stack --> 재귀함수, arguments(graph,start,visited)
bfs : queue --> from collections import deque, 항상 queue인걸 확인하고 항상 [output<--input] 기억! (popleft())
# 재귀 함수
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,v,visited):
visited[v] = True
print(v, end=" ")
new_visit = deque([graph[v]])
while new_visit:
value = new_visit.popleft()
for i in value:
if not visited[i]:
print(i, end=" ")
new_visit.append(graph[i])
visited[i] = True
from collections import defaultdict,deque
node, edge, v = list(map(int,input().split()))
visited = [False] * (node + 1)
graph = defaultdict(list)
for i in range(1,edge+1):
start,end = list(map(int,input().split()))
graph[start].append(end)
graph[end].append(start)
for key, value in graph.items():
graph[key] = sorted(value)
visited = [False] * (node + 1)
dfs(graph,v,visited)
print()
visited = [False] * (node + 1)
bfs(graph,v,visited)