백준 - 그래프(# 11725)

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


Code

import sys
sys.setrecursionlimit(10**9)

def traversal(parent):
    checked[parent] = 1
    for node in tree[parent]:
        if checked[node] == 0:
            result[node] = parent
            traversal(node)

n = int(input())
result = [0]*(n+1)
checked = [0]*(n+1)
tree = {x:[] for x in range(1,n+1)}

for _ in range(n-1):
    x, y = map(int, sys.stdin.readline().split())
    tree[x].append(y)
    tree[y].append(x)

traversal(1)
for i in result[2:]:
    print(i)

좋은 웹페이지 즐겨찾기