-
Notifications
You must be signed in to change notification settings - Fork 0
/
1260.py
58 lines (45 loc) · 1.1 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
from collections import deque
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 range(len(graph)):
graph[i].sort()
dfs_result = []
bfs_result = []
def dfs(x, visited):
visited[x] = True
dfs_result.append(x)
for g in graph[x]:
if visited[g]:
continue
dfs(g, visited)
def bfs(x, visited):
q = deque()
q.append(x)
while q:
x = q.popleft()
if visited[x]:
continue
visited[x] = True
bfs_result.append(x)
for g in graph[x]:
if visited[g]:
continue
q.append(g)
visited = [False] * (n+1)
dfs(v, visited)
visited = [False] * (n+1)
bfs(v, visited)
for i in range(len(dfs_result)):
if i == len(dfs_result) - 1:
print(dfs_result[i])
else:
print(dfs_result[i], end=" ")
for i in range(len(bfs_result)):
if i == len(bfs_result) - 1:
print(bfs_result[i])
else:
print(bfs_result[i], end=" ")