문제 링크
백준 1260 DFS와 BFS 링크
문제 풀이
내 코드
import sys
from _collections import deque
N, M, V = map(int, sys.stdin.readline().split())
graph = [[] for i in range(N+1)]
for i in range(M):
a, b = map(int, sys.stdin.readline().split())
graph[a].append(b)
graph[b].append(a)
for i in range(N + 1):
graph[i].sort()
visit = [False for i in range(N+1)]
def dfs(num):
visit[num] = True
print(num, end=' ')
for i in graph[num]:
if visit[i] == False:
dfs(i)
dfs(V)
print('')
visit = [False for i in range(N+1)]
myQue = deque()
def bfs(num):
myQue.append(num)
visit[num] = True
print(num, end = ' ')
while myQue:
x = myQue.popleft()
for i in graph[x]:
if visit[i] == False:
myQue.append(i)
print(i, end=' ')
visit[i] = True
bfs(V)