-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1260번.py
More file actions
39 lines (32 loc) · 765 Bytes
/
1260번.py
File metadata and controls
39 lines (32 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#DFS와 BFS 프로그램
from collections import deque
N, M, Start = map(int, input().split())
A = [[] for _ in range(N+1)]
for _ in range(M):
s, e = map(int, input().split())
A[s].append(e)
A[e].append(s)
for i in range(N+1):
A[i].sort()
def DFS(v):
print(v, end=' ')
visited[v] = True
for i in A[v]:
if (visited[i] == False):
DFS(i)
visited = [False] * (N+1)
DFS(Start)
def BFS(v):
queue = deque()
queue.append(v)
visited[v] = True
while queue:
now_Node = queue.popleft()
print(now_Node, end=' ')
for i in A[now_Node]:
if (visited[i] == False):
visited[i] = True
queue.append(i)
print()
visited = [False] * (N+1)
BFS(Start)