- DFS : 재귀
- BFS : 반복
함수 안에 넣고 소팅 해야 함
전형적인 DFS, BFS 문제였음
from collections import deque
n, m, v = map(int, input().split())
Dvisited = [False] * (n+1)
Bvisited = [False] * (n+1)
graph = [[] for _ in range(n+1)]
for i in range(m):
x, y = map(int, input().split())
graph[x].append(y)
graph[y].append(x)
for i in range(1, n+1):
graph[i] = sorted(graph[i])
def dfs(graph, k, Dvisited):
Dvisited[k] = True
print(k, end =' ')
for i in graph[k]:
if not Dvisited[i]:
dfs(graph, i, Dvisited)
def bfs(graph, start, Bvisited):
queue = deque([start])
Bvisited[start] = True
while queue:
h = queue.popleft()
print(h, end = ' ')
for i in graph[h]:
if not Bvisited[i]:
queue.append(i)
Bvisited[i] = True
dfs(graph, v, Dvisited)
print()
bfs(graph, v, Bvisited)