[백준/파이썬] 1260번

민정·2023년 3월 9일
0

[백준/파이썬]

목록 보기
117/245
post-thumbnail

백준 1260번

문제

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

코드

from collections import deque

def dfs(graph, v, visited):
    visited[v] = True
    print(v, end = " ")
    for i in graph[v]:
        if not visited[i]:
            dfs(graph, i, visited)

def bfs(graph, s, visited):
    que = deque([s])
    visited[s] = True
    while que:
        v = que.popleft()
        print(v, end = " ")
        for i in graph[v]:
            if not visited[i]:
                que.append(i)
                visited[i] = True

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

graph = [[] for _ in range(n+1)]

for _ in range(m):
    temp_li = list(map(int, input().split()))
    graph[temp_li[0]].append(temp_li[1])
    graph[temp_li[1]].append(temp_li[0])

for i in range(n+1):
    graph[i].sort()

visited = [False]*(n+1) 
dfs(graph,v,visited) 
print() 
visited = [False]*(n+1)
bfs(graph,v,visited) 
profile
パㅔバ6ㅇr 덤벼ㄹΓ :-0

0개의 댓글