Skip to content

Commit

Permalink
[BOJ] 1260번 DFS와 BFS (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
heerucan committed Feb 15, 2022
1 parent 2af30d5 commit 826f169
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions BOJ/BFS:DFS/1260.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from collections import deque

n, m, v = map(int, input().split())

g = [[0] * (n + 1) for _ in range(n + 1)]
visited= [False] * (n+1)

for _ in range(m):
a, b = map(int, input().split())
g[a][b] = 1
g[b][a] = 1


def dfs(v):
visited[v] = True
print(v, end = ' ')
for i in range(1, n+1):
if not visited[i] and g[v][i] == 1:
dfs(i)

def bfs(v):
visited[v] = False
queue = [v]
while queue: # queue가 채워져있는 동안에만 해당 while문이 돌아감
v = queue.pop(0)
print(v, end = ' ')
for i in range(n+1):
if visited[i] and g[v][i] == 1:
queue.append(i)
visited[i] = False

dfs(v)
print()
bfs(v)

0 comments on commit 826f169

Please sign in to comment.