아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Kitten on a Tree

면접 대비

시간 제한1초메모리 제한1024 MB

요약
부모에서 자식으로 향하는 간선 목록으로 주어진 트리에서 시작 지점부터 루트까지 내려가는 경로를 출력한다.
난이도

보통10점 중 4점

유형
트리, DFS, 그래프, 구현
정답자
아직 제출이 없습니다

문제

Ouch! A kitten got stuck on a tree.  Fortunately, the tree's branches are numbered. Given a description of a tree and the position of the kitten, can you write a program to help the kitten down?

입력

The input is a description of a single tree. The first line contains an integer KK, denoting the branch on which the kitten got stuck. The next lines each contain two or more integers a,b_1,b_2,…a, b\_1, b\_2, \ldots. Each such line denotes a branching: the kitten can reach aa from b_1,b_2,…b\_1, b\_2, \ldots on its way down. Thus, aa will be closer to the root than any of the b_ib\_i. The description ends with a line containing -1. Each branch b_ib\_i will appear on exactly one line. All branch numbers are in the range 1..1001..100, though not necessarily contiguous. You are guaranteed that there is a path from every listed branch to the root. The kitten will sit on a branch that has a number that is different than the root.

The illustration above corresponds to the sample input.

출력

Output the path to the ground, starting with the branch on which the kitten sits.

예제1

  1. 예제 1

    입력
    14
    25 24
    4 3 1 2
    13 9 4 11
    10 20 8 7
    32 10 21
    23 13 19 32 22
    19 12 5 14 17 30
    14 6 15 16
    30 18 31 29
    24 23 26
    26 27 28
    -1
    
    예상 출력
    14 19 23 24 25