-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1260.py
45 lines (35 loc) · 836 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
from __future__ import print_function
import sys
from collections import deque
def dfs(v):
visited.append(v)
for i in adj[v]:
if i not in visited:
dfs(i)
def bfs(v):
need_visit = deque()
need_visit.append(v)
while need_visit:
node = need_visit.popleft()
if node not in visited:
visited.append(node)
for j in adj[node]:
if j not in visited:
need_visit.append(j)
n,m,v = map(int, sys.stdin.readline().split())
adj = [[] for _ in range(n + 1)]
for _ in range(m):
x, y = map(int, sys.stdin.readline().split())
adj[x].append(y)
adj[y].append(x)
for e in adj:
e.sort()
visited = []
dfs(v)
for i in visited:
print(i, end = " ")
print()
visited = []
bfs(v)
for j in visited:
print(j, end= " ")