최종코드(코드링크)
import sys
from sys import setrecursionlimit
from collections import defaultdict
setrecursionlimit(10**9)
# input = sys.stdin.readline
N = int(input())
visit = [False] * (N + 1)
tree = defaultdict(list)
for _ in range(N - 1):
a, b = map(int, input().split())
tree[a].append(b)
tree[b].append(a)
parents = [0] * (N + 1)
def find(node):
visit[node] = True
node_list = tree[node]
for child in node_list:
if not visit[child]:
parents[child] = node
find(child)
find(1)
print('\n'.join(map(str, parents[2:])))
아무리 제출해도 런타임 에러가 발생하였는데..
setrecursionlimit(10**9)를 작성하여 해결했습니다 !
'알고리즘 문제 풀이' 카테고리의 다른 글
[백준 - Python] 32399. 햄버거 정렬 (0) | 2024.10.28 |
---|---|
[백준 - Python] 13549. 숨바꼭질 3 (0) | 2024.10.15 |
[백준 - Python] 28270. Marked-Numbered (0) | 2024.10.12 |
[백준 - Python] 27966. △ (0) | 2024.10.11 |
[백준 - Python] 14244. 트리 만들기 (0) | 2024.09.24 |