-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1260.py
46 lines (35 loc) · 990 Bytes
/
1260.py
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
from collections import deque
def dfs(graph, start):
visited = []
to_visit = deque([start])
while to_visit:
node = to_visit.pop()
if node not in visited:
visited.append(node)
to_visit.extend(sorted(graph[node]).__reversed__())
return visited
def bfs(graph, start):
visited = []
to_visit = deque([start])
while to_visit:
node = to_visit.popleft()
if node not in visited:
visited.append(node)
to_visit.extend(sorted(graph[node]))
return visited
n, m, v = map(int, input().split())
graph = {}
for i in range(m):
v1, v2 = map(int, input().split())
if v1 not in graph.keys():
graph[v1] = []
if v2 not in graph.keys():
graph[v2] = []
graph[v1].append(v2)
graph[v2].append(v1)
if v not in graph.keys():
print(v)
print(v)
exit()
print(' '.join(str(s) for s in dfs(graph, v)))
print(' '.join(str(s) for s in bfs(graph, v)))