https://www.acmicpc.net/problem/1260
내 코드
중간에 link 를 sort하고 했다가 예제는 다 맞는데 제출이 틀려서 스터디원에게 검토받고
graph sort 해야하는걸 겨우 찾았다....
그마저도 바로 못이해해서 예외를 알려주고 이해한..
사랑해요 스터디원♥ 보고있나요
import sys
from collections import deque
input = sys.stdin.readline
N, M, V = map(int, input().split())
link = []
for _ in range(M):
link.append(list(map(int, input().split())))
graph = [[] for _ in range(N+1)]
visited = [False] * (N+1)
for i in link:
a, b = i
graph[a].append(b)
graph[b].append(a)
for i in graph:
i.sort()
def dfs(graph, V, visited):
visited[V] = True
print(V, end = ' ')
for i in graph[V]:
if not visited[i]:
dfs(graph, i, visited)
dfs(graph, V, visited)
print()
visited = [False] * (N+1)
def bfs(graph, V, visited):
queue = deque()
queue.append(V)
visited[V] = True
while queue:
V = queue.popleft()
print(V, end = ' ')
for i in graph[V]:
if not visited[i]:
queue.append(i)
visited[i] = True
bfs(graph, V, visited)
'코테 공부 > python' 카테고리의 다른 글
[백준] #11404 Python 플로이드 워셜 (0) | 2023.07.05 |
---|---|
[백준] #11403 Python 세미? 플로이드 워셜 (2) | 2023.06.30 |
[프로그래머스] 같은 숫자는 싫어 -stack (0) | 2023.06.28 |
[백준] #2583 Python 영역구하기 -bfs (0) | 2023.06.28 |
Python DFS BFS (0) | 2023.06.21 |