https://www.acmicpc.net/problem/1260
ํ์ด
# ๋ฐฑ์ค 1260๋ฒ ๋ฌธ์ - DFS์ BFS
# Depth First Search
def dfs(n):
print(n, end=' ')
visited[n] = True
for i in graph[n]:
if not visited[i]:
dfs(i)
# Breadth First Search
def bfs(n):
visited[n] = True
queue = deque([n])
while queue:
v = queue.popleft()
print(v, end= ' ')
for i in graph[v]:
if not visited[i]:
queue.append(i)
visited[i] = True
import sys
from collections import deque
# node, branch, first node
n, m, v = map(int, sys.stdin.readline().split())
graph = [[] for _ in range(n+1)]
visited = [False] * (n + 1)
# make adjacency list
for _ in range(m):
a, b = map(int, sys.stdin.readline().split())
graph[a].append(b)
graph[b].append(a)
# sort adjacency list
for i in range(1, n+1):
graph[i].sort()
dfs(v)
# initialize check list
visited = [False] * (n + 1)
print()
bfs(v)
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] [Python] 13305๋ฒ_์ฃผ์ ์ (0) | 2022.03.16 |
---|---|
[๋ฐฑ์ค] [Python] 1789๋ฒ_์๋ค์ ํฉ (0) | 2022.03.16 |
[๋ฐฑ์ค] [Python] 11651๋ฒ_๋์ด์ ์ ๋ ฌ (0) | 2022.03.14 |
[๋ฐฑ์ค] [Python] 10814๋ฒ_๋์ด์ ์ ๋ ฌ (0) | 2022.03.13 |
[๋ฐฑ์ค] [Python] 11650๋ฒ_์ขํ ์ ๋ ฌํ๊ธฐ (0) | 2022.02.26 |