Notice
Recent Posts
Recent Comments
Link
나의 개발일지
[백준] 24479 알고리즘 수업 - 깊이 우선 탐색 1 [Python, 파이썬] 본문
- 문제 : https://www.acmicpc.net/problem/24479
- 🔑 visit 리스트에 방문했으면 1 대신 방문 순서를 저장
- 시작 노드가 가장 첫 방문 순서이기 때문에 1로 설정하고 cnt 변숫값을 2부터 시작
import sys
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
n, m, r = map(int, input().split())
graph = {i : [] for i in range(1, n+1)}
for i in range(m):
p, c = map(int, input().split())
graph[p].append(c)
graph[c].append(p)
for i in graph:
graph[i].sort()
visit = [0] * (n+1)
visit[r] = 1
cnt = 2
def dfs(p):
global cnt
for c in graph[p]:
if visit[c] == 0:
visit[c] = cnt
cnt += 1
dfs(c)
dfs(r)
for i in range(1, n+1):
print(visit[i])
'백준' 카테고리의 다른 글
[백준] 1238 파티 [Python, 파이썬] (1) | 2023.11.01 |
---|---|
[백준] 5557 1학년 [Python, 파이썬] (1) | 2023.10.24 |
[백준] 11725 트리의 부모 찾기 [Python, 파이썬] (0) | 2023.10.21 |
[백준] 16118 달빛 여우 [Python, 파이썬] (1) | 2023.10.21 |
[백준] 1753 최단경로 [Python, 파이썬] (2) | 2023.10.20 |
Comments