-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1260.py
More file actions
39 lines (34 loc) · 702 Bytes
/
1260.py
File metadata and controls
39 lines (34 loc) · 702 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
#20250716
#1260 DFS와 BFS
#DFS,BFS
from collections import deque
n,m,v=map(int,input().split())
a=[[] for _ in range(n+1)]
visited=[False]*(n+1)
for _ in range(m):
s,e=map(int, input().split())
a[e].append(s)
a[s].append(e)
for i in range(n+1):
a[i].sort()
def DFS(v):
print(v,end=' ')
visited[v]=True
for i in a[v]:
if not visited[i]:
DFS(i)
def BFS(v):
queue=deque()
queue.append(v)
visited[v]=True
while queue:
now=queue.popleft()
print(now,end=' ')
for i in a[now]:
if not visited[i]:
visited[i]=True
queue.append(i)
DFS(v)
print()
visited=[False]*(n+1)
BFS(v)