BOJ - 11725 - 트리의 부모 찾기

Updated:

import sys
sys.setrecursionlimit(10 ** 9)

def DFS(start, tree, parents):
    for i in tree[start]:
        if parents[i] == 0:
            parents[i] = start
            DFS(i, tree, parents)

def solution():
    N = int(input())

    tree = [[] for _ in range(N+1)]
    for _ in range(N-1):
        start, end = map(int, sys.stdin.readline().split())
        tree[start].append(end)
        tree[end].append(start)

    parents = [0 for _ in range(N+1)]

    DFS(1, tree, parents)
    for i in range(2, N+1):
        print(parents[i])

solution()

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

Categories:

Updated:

Leave a comment