-
코드
n, m, v = map(int, input().split()) tree = list([] for _ in range(1001)) count = list(0 for _ in range(1001)) dfs_check = [0 for _ in range(1001)] dfs_answer = [] bfs_check = [0 for _ in range(1001)] bfs_answer = [] que = [] for _ in range(m): between = list(map(int, input().split())) tree[between[0]].append(between[1]) tree[between[1]].append(between[0]) count[between[0]] += 1 count[between[1]] += 1 def dfs(tree, number): if dfs_check[number] == 0: dfs_answer.append(number) dfs_check[number] = 1 tree[number].sort() for i in range(count[number]): dfs(tree, tree[number][i]) def bfs(tree): while len(que) > 0: x = que.pop(0) bfs_answer.append(x) bfs_check[x] = 1 tree[x].sort() for i in range(count[x]): if bfs_check[tree[x][i]] == 0: que.append(tree[x][i]) bfs_check[tree[x][i]] = 1 que.append(v) bfs(tree) dfs(tree, v) print(*dfs_answer) print(*bfs_answer)
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 2667번 단지번호붙이기 (파이썬) (0) 2022.01.04 [백준] 2606번 바이러스 (파이썬) (0) 2022.01.04 [백준] 7579번 앱 (파이썬) (0) 2022.01.04 [백준] 2293번 동전 1 (파이썬) (0) 2022.01.04 [백준] 2629번 양팔저울 (파이썬) (0) 2022.01.04