We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2af30d5 commit 826f169Copy full SHA for 826f169
BOJ/BFS:DFS/1260.py
@@ -0,0 +1,34 @@
1
+from collections import deque
2
+
3
+n, m, v = map(int, input().split())
4
5
+g = [[0] * (n + 1) for _ in range(n + 1)]
6
+visited= [False] * (n+1)
7
8
+for _ in range(m):
9
+ a, b = map(int, input().split())
10
+ g[a][b] = 1
11
+ g[b][a] = 1
12
13
14
+def dfs(v):
15
+ visited[v] = True
16
+ print(v, end = ' ')
17
+ for i in range(1, n+1):
18
+ if not visited[i] and g[v][i] == 1:
19
+ dfs(i)
20
21
+def bfs(v):
22
+ visited[v] = False
23
+ queue = [v]
24
+ while queue: # queue가 채워져있는 동안에만 해당 while문이 돌아감
25
+ v = queue.pop(0)
26
27
+ for i in range(n+1):
28
+ if visited[i] and g[v][i] == 1:
29
+ queue.append(i)
30
+ visited[i] = False
31
32
+dfs(v)
33
+print()
34
+bfs(v)
0 commit comments