-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1260.py
More file actions
64 lines (53 loc) · 1.28 KB
/
1260.py
File metadata and controls
64 lines (53 loc) · 1.28 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import sys
from collections import deque
input = sys.stdin.readline
n, m, v = map(int, input().split())
data = [[0]*(n+1) for _ in range(n+1)]
#입력받은 graph array로 만들기
for _ in range(m):
x, y = map(int, input().split())
data[x][y] = 1
data[y][x] = 1
#DFS 구현
'''def dfs(graph, root):
visited = []
stack = [root]
while stack :
now = stack.pop()
if now not in visited :
visited.append(now)
stack.extend(graph[now])
return visited'''
visited = [False]*(n+1)
def dfs(now):
visited[now] = True
print(now, end=' ')
for i in range(1, n+1):
if data[now][i] == 1 and visited[i] is False:
dfs(i)
#BFS 구현
'''def bfs(graph, root):
visited = []
queue = [root]
while queue :
now = queue.pop(0)
if now not in visited :
visited.append(now)
queue.extend(graph[now])
return visited'''
def bfs(now):
visited = [False]*(n+1)
q = deque()
q.append(now)
visited[now] = True
while q:
now = q.popleft()
print(now, end=' ')
for i in range(1, n+1):
if data[now][i] == 1 and visited[i] is False:
visited[i] = True
q.append(i)
dfs(v)
print()
bfs(v)
print()