-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ1260.py
More file actions
49 lines (36 loc) · 703 Bytes
/
BJ1260.py
File metadata and controls
49 lines (36 loc) · 703 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
42
43
44
45
46
47
48
49
from collections import deque
NMV = input().split(' ')
graph = [[] for _ in range(1001)]
visited = [0 for _ in range(1001)]
visited2 = [0 for _ in range(1001)]
N = int(NMV[0])
M = int(NMV[1])
V = int(NMV[2])
for i in range(M):
edge = input().split(' ')
a = int(edge[0])
b = int(edge[1])
graph[a].append(b)
graph[b].append(a)
for i in graph:
i.sort()
## DFS
def dfs(cur):
visited[cur] = 1
print(cur, end=' ')
for i in graph[cur]:
if not visited[i]:
dfs(i)
dfs(V)
print()
# BFS
q = deque()
q.append(V)
while len(q) > 0:
a = q.popleft()
if not visited2[a]:
print(a, end=' ')
visited2[a] = 1
for i in graph[a]:
if not visited2[i]:
q.append(i)