-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |