-
Notifications
You must be signed in to change notification settings - Fork 0
/
1260.py
59 lines (45 loc) · 1.04 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
59
import sys
from collections import deque
def dfs(v):
visited[v]=True
print(v,'',end='')
for i in adj_list[v]:
if not visited[i]:
visited[i]=True
dfs(i)
def bfs(i):
queue=[]
queue.append(i)
visited[i]=True
while len(queue)!=0:
v=queue.pop(0)
print(v,'',end='')
for w in adj_list[v]:
if not visited[w]:
visited[w]=True
queue.append(w)
N,M,V=map(int,sys.stdin.readline().split())
adj_list=[[]for i in range(1001)]
visited=[False]*(1001)
for i in range(M):
a,b=map(int,sys.stdin.readline().split())
adj_list[a].append(b)
adj_list[b].append(a)
adj_list[a].sort()
adj_list[b].sort()
#dfs
'''
for i in range(V,1001):
if not visited[i] and len(adj_list[i])!=0:
dfs(i)
'''
dfs(V)
print()
#bfs
visited=[False]*(1001)
bfs(V)
'''
for i in range(V,1001):
if not visited[i] and len(adj_list[i])!=0:
bfs(i)
'''