728x90
반응형
문제출처: https://www.acmicpc.net/problem/1260
풀이:
import sys
from collections import deque
sys.stdin = open('/Users/song/Desktop/Python/Python/h.txt', 'r')
def bfs(v):
queue = deque([v])
visited[v] = True
while queue:
q = queue.popleft()
print(q, end=" ")
for i in graph[q]:
if not visited[i]:
visited[i] = True
queue.append(i)
def dfs(v):
visited[v] = True
print(v,end=" ")
for i in graph[v]:
if not visited[i]:
dfs(i)
N, M, V = map(int, input().split())
graph = [[] for _ in range(N + 1)]
for i in range(M):
start, end = map(int,input().split(" "))
graph[start].append(end)
graph[end].append(start)
for i in graph:
i.sort()
visited = [False] * (N+1)
dfs(V)
print()
visited = [False] * (N+1)
bfs(V)
dfs 와 bfs 기본 다져가는중
반응형
댓글