-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy path[14]_1.py
More file actions
35 lines (30 loc) · 682 Bytes
/
[14]_1.py
File metadata and controls
35 lines (30 loc) · 682 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
from collections import deque
def dfs(v):
print(v, end=' ')
visited[v] = True
for e in adj[v]:
if not(visited[e]):
dfs(e)
def bfs(v):
q = deque([v])
while q:
v = q.popleft()
if not(visited[v]):
visited[v] = True
print(v, end=' ')
for e in adj[v]:
if not visited[e]:
q.append(e)
n, m, v = map(int, input().split())
adj = [[] for _ in range(n + 1)]
for _ in range(m):
x, y = map(int, input().split())
adj[x].append(y)
adj[y].append(x)
for e in adj:
e.sort()
visited = [False] * (n + 1)
dfs(v)
print()
visited = [False] * (n + 1)
bfs(v)