-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1260.py
More file actions
41 lines (30 loc) · 725 Bytes
/
1260.py
File metadata and controls
41 lines (30 loc) · 725 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
40
41
from collections import deque
def dfs(start):
visited[start] = True
print(start, end=" ")
for i in graph[start]:
if not visited[i]:
dfs(i)
def bfs(start):
queue = deque([start])
visited[start] = True
while queue:
v = queue.popleft()
print(v, end=" ")
for i in graph[v]:
if not visited[i]:
visited[i] = True
queue.append(i)
N, M, V = map(int, input().split())
graph = [[] for _ in range(N + 1)]
for _ in range(M):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
for i in graph:
i.sort()
visited = [False] * (N + 1)
dfs(V)
print()
visited = [False] * (N + 1)
bfs(V)