• Home
  • About
    • JOOS photo

      JOOS

      Joos's blog

    • Learn More
    • Email
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

[백준] 1260번 : DFS와 BFS with python3

08 Nov 2018

Reading time ~1 minute

문제: https://www.acmicpc.net/problem/1260

목표:

이 문제는 dfs, bfs에 대한 이해가 중요한 것 같다.
관련 정보가 들어간 url 은 https://joosjuliet.github.io/dfs_vs_bfs/ 이다

n, m, v = map(int, input().split())

vertexs = {}
for i in range(1, n+1):
    vertexs[i] = set()

for _ in range(m):
    a, b = map(int, input().split())
    vertexs[a].add(b)
    vertexs[b].add(a)

check = [False]*(n+1)

def dfs(n):
    print(n,end=' ')
    check[n] = True
    for k in sorted(vertexs[n]):
        if not check[k]:
            dfs(k)

dfs(v)

print()
check = [False]*(n+1)
# 이미 간 곳 확인
queue = [v]
check[v] = True
print(v,end=' ')
while queue:
    v = queue.pop(0)
    for u in sorted(vertexs[v]):
        if not check[u] :
            print(u,end=' ')
            queue.append(u)
            check[u] = True



algorithm백준 Share Tweet +1