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
|
from collections import deque
import sys
n,m,start = map(int, input().split())
com = []
graph = [[0]]
visited = [False] * (n+1)
result_b = []
result_d = []
for i in range(m):
co = list(map(int,sys.stdin.readline().split()))
co.sort()
com.append(co)
for k in range(n):
graph.append([])
for l in range(m):
if k+1 in com[l]:
if k+1==com[l][0]:
graph[k+1].append(com[l][1])
else:
graph[k + 1].append(com[l][0])
for i in graph:
i.sort()
def bfs(Graph, s, Visited):
queue = deque([s])
Visited[s] = True
while queue:
v = queue.popleft()
result_b.append(v)
for i in Graph[v]:
if Visited[i] is False:
queue.append(i)
Visited[i] = True
def dfs(Graph, v, Visited):
Visited[v] = True
result_d.append(v)
for i in Graph[v]:
if Visited[i] is False:
dfs(Graph, i, Visited)
visited_b = list(visited)
visited_d = list(visited)
dfs(graph,start, visited_d)
for i in result_d:
print(i, end=" ")
print()
bfs(graph, start, visited_b)
for i in result_b:
print(i, end=" ")
|
cs |
'백준' 카테고리의 다른 글
백준 11660: 구간 합 구하기 5 (1) | 2023.01.20 |
---|---|
백준 11659번: 구간 합 구하기 4 (0) | 2023.01.19 |
백준 10828번: 스택 (0) | 2021.09.30 |
백준 10845번: 큐 (0) | 2021.09.30 |
백준 10866번: 덱 (0) | 2021.09.30 |