https://www.acmicpc.net/problem/1260
from collections import deque
import sys
input=sys.stdin.readline
n, m, v = map(int, input().split())
visited=[0] * (n+1)
graph=[[0]*(n+1) for _ in range(n+1)]
for _ in range(m):
a, b=map(int, input().split())
graph[a][b]=1
graph[b][a]=1
def dfs(v):
visited[v]=1
print(v, end=' ')
for i in range(1, n+1):
if visited[i]==0 and graph[v][i]==1:
dfs(i)
def bfs(v):
queue=deque([v])
visited[v]=0
while queue:
v=queue.popleft()
print(v, end=' ')
for i in range(1, n+1):
if visited[i]==1 and graph[v][i]==1:
queue.append(i)
visited[i]=0
dfs(v)
print()
bfs(v)
'알고리즘 > 그래프' 카테고리의 다른 글
[백준/그래프] 13023 ABCDE (Python, 파이썬) (0) | 2021.06.19 |
---|